This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 5 years ago.
{
"quote": "To understand the heart and mind of a person, look not at
what he has already achieved, but at what he aspires to.",
"author": "Kahlil Gibran (1883-1931)"
}
How to parse this with no any object name
There are different methods to parse json object
Simple way
JSONObject myJson = new JSONObject(yourJsonString);
String quote = myJson. getString("quote");
String author = myJson. getString("author");
Another and recomented way is parse json using Gson
For this you can refer below links
How to parse json parsing Using GSON in android
https://www.journaldev.com/2321/gson-example-tutorial-parse-json
How to do it ?
If you meet {} in your code , you can use JSONObject to parse it .
If you meet [] in your code , you can use JSONArray to parse it .
And if you meet [] in your code , you can use for loop to get value in it .
And you should use try catch in your code .
Try this .
try {
JSONObject jsonObject = new JSONObject(response);
String author = jsonObject.getString("author");
String quote = jsonObject.getString("quote");
} catch (JSONException e) {
e.printStackTrace();
}
This is absolutely normal json.
JSONObject json = new JSONObject(your_json);
String quote = json.getString("quote");
String author = json.getString("author");
Related
This question already has answers here:
Parsing JSON Object in Java [duplicate]
(5 answers)
Closed 5 years ago.
this is my json response
{
"status": true,
"code": 200,
"message": "Success",
"data": {
"id": "14"
}
}
how to read this values
You can use the help of Google's GSON library built for exact your pupose.
YourPojoClass model = new Gson().fromJson(jsonResponse, YourPojoClass.class);
Convert your json to PojoModel by using jsonschema2pojo.org
You can do it like this:
Create a JSONObject:
JSONObject jObject = new JSONObject(result);
To get a specific boolean
String aJsonString = jObject.getBoolean("status");
To get a specific String
String aJsonString = jObject.getString("message");
To get a specific Integer
int aJsonInteger = jObject.getInt("code");
This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 6 years ago.
I get a response but it's different for every user.
I want to extract the UserID & Name. How can I do this?
The response String:
{
"sessionkey":"f4b54dedlfkjhgdlfkjghdfslkjgh1a255242bf71597f4b35ac882f0702",
"user" : {
"userID" : "1",
"name":"Test"
}
}
I want to save 1 = (String userid)
and save Test = (String name)
Thanks in advance
This is just a simple JSON blob, so use a JSON Parser to extract the data.
My lib of choice is org.json.json.
String blob = "{ origin string }";
// create a new json object from the main blob
JSONObject root = new JSONObject(blob);
// extract the session key
String sessionKey = root.getString("sessionkey");
// extract the user object
JSONObject user = root.getJSONObject("user");
// extract the user id
String userId = user.getString("userID");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I am getting Exception that json object cannot be converted to json array..,when I am showing values from server, here is my code. Please help me.
if(respons!=null){
try{
JSONObject jsonObj=new JSONObject(respons);
JSONArray post=jsonObj.getJSONArray("List of image URLs");
for(int i =0;i<post.length();i++){
String values = post.getString(i);
_issueList.add(values);
}
}
Here is my json exception at this output
{
"message": [
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389346961Quincredible_1-
2.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389346977Quincredible_1-
3.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389346996Quincredible_1-
4.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389347016Quincredible_1-
5.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389347039Quincredible_1-
6.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389347052Quincredible_1-
7.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389347062Quincr32.png"
]
}
This data is coming from server. I don't know why this error is occurring, my images are not showing and moving.
Please help me. Thank you.
if(respons!=null){
try{
JSONObject jsonObj=new JSONObject(respons);
JSONArray post=jsonObj.getJSONArray("message");
for(int i =0;i<post.length();i++){
String values = post.getString(i);
_issueList.add(values);
}
change to:
if(respons!=null){
try{
JSONObject jsonObj=new JSONObject(respons);
JSONArray post=jsonObj.getJSONArray("message");
for(int i =0;i<post.length();i++){
String values = post.getString(i);
_issueList.add(values);
}
}
key for your json array is message not List of image URLs
Change your below line in which you need to get the arraylist of message where you have written wrong key. Other code is fine.
JSONArray post=jsonObj.getJSONArray("List of image URLs");
Change it to
JSONArray post=jsonObj.getJSONArray("message");
As you are getting only the JSONArray in your response there is not need of JSONObject in your code.
You can parse your array as below which will directly give you an array :
try{
JSONArray post=new JSONArray(respons);
for(int i =0;i<post.length();i++){
String values = post.getString(i);
_issueList.add(values);
}
I have this JSON string that I am recieving after I make a purchase from the playstore and I am apparently missing something in my code when I am trying to parse it.
PurchaseInfo:{"orderId":"12999763165555505758.1317333586444405",
"packageName":"com.mypkgname.myapp",
"productId":"monthly_purchase_01",
"purchaseTime":1357456489000,
"purchaseState":0,
"purchaseToken":"yrynypfkdncvhlxdbypysvwz.AO-J1OxFkndfqkClAqbbYAOApkMgTG4VX9Ef0uNP0FIs9-xGrXivkbx3FNMA2yNU12K_sbvRGFcknVBTfisI-uZawCXLGlMX4v4Zw8GFOmS0Q6PIbiITTGqn5h1QbEB4Rv84sXdUJHP3B_UQfujZN7ADi9bm_N4_iA"}
Here is the snippet of code I am using it to attempt the parsing
try {
JSONObject j1 = new JSONObject(tester1);
JSONArray mPurchInfo = j1.getJSONArray("PurchaseInfo");
int count = mPurchInfo.length();
final String[] purchInfo = new String[count];
JSONObject q1 = mPurchInfo.getJSONObject(0);
purchInfo[0] = q1.getString("orderId");
purchInfo[1] = q1.getString("packageName");
purchInfo[2] = q1.getString("productId");
purchInfo[3] = q1.getString("purchaseTime");
purchInfo[4] = q1.getString("purchaseState");
purchInfo[5] = q1.getString("purchaseToken");
orderID=purchInfo[0];
} catch (JSONException e) {
Log.e("Error","Yes");
}
I am catching the an error as I see this last log statement in my log but I am still trying to learn the parsing JSON Strings
I hope I am at least close
Ideally I would like to have Strings set to all the values in the JSON String
orderID = ??
packageName = ??
etc.
Thanks
I have this JSON string that I am recieving after I make a purchase from the playstore
If that's really what you get back, you need to contact them and tell them to fix their API; that string is not valid JSON. There are two problems with it:
A JSON document must have an object or array as the top-level item. The string as quoted is missing a { at the beginning and a } at the end.
All keys in JSON must be in double quotes. The first one, PurchaseInfo, is not.
Also, your code is doing this:
JSONArray mPurchInfo = j1.getJSONArray("PurchaseInfo");
...but if it were valid JSON, PurchaseInfo wouldn't be an array, it'd be an object.
Looking at it, if you remove the PurchaseInfo: at the beginning, it's valid. Once you've removed that, this line:
JSONObject j1 = new JSONObject(tester1);
...will give you an object from which you can query information:
String orderId = j1.getString("orderId");
hello i have a JSON data format can anyone please help me to make dynamic JSONStringer object for this String
{"Text":"Hello Simple Text",
"Files":[{"ContentType":"image/png",
"Content":"iVBORw0KGgoAAAANSUhEUgAAAR8AAACMCAIAAADKsDpDAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAB3RJTUUH2wYWDzIB3zSYdQAAAAd0RVh0QXV0aG9yAKmuzEgAAAAMdEVYdERlc2NyaXB0aW9uABMJISMAAAAKdEVYdENvcHlyaWdodACsD8w6AAAADnRFWHRDcmVhdGlvbiB0aW1lADX3DwkAAAAJdEVYdFNvZnR3YXJlAF1w/zoAAAALdEVYdERpc2NsYWltZXIAt8C0jwAAAAh0RVh0V2FybmluZwDAG+aHAAAAB3RFWHRTb3VyY2UA9f+D6wAAAAh0RVh0Q29tbWVudAD2zJa/AAAABnRFWHRUaXRsZQCo7tInAAABAElEQVR4nO2de1zUVf7/3+dzmwsMoCgDXgARBO/"}],
"AuthToken":"XkWQRd65+H+iPtlOoAEYAR0jrzB1o3UV"}
i have used
jsonstr = new JSONStringer().object().key("Text")
.value(msg).key("Files").array().object().key(
"ContentType").value("image/png").key(
"Content").value(enimg)
.endObject().endArray().key("AuthToken").value(token)
.endObject();
but the server is giving me fault message in return, not accepting the data.
actually i was doing the right thing..everything was OK..
the problem was with org.json package it was not accurate with Base64 string
i switched to another library and all worked..
https://stackoverflow.com/questions/338586/a-better-java-json-library
see the above question for another json libraries
that was problem with org.json
i switched to another..and all it works
nesting too deep in JSON... should I switch to XML?
This is a way to do what you want:
// Creating root JSONObject
JSONObject json = new JSONObject();
// Put in it a String field
json.put("Text", "Hello sample");
// Creating a JSONArray
JSONArray arr = new JSONArray();
//Creating the element to populate the array
JSONObject element = new JSONObject();
element.put("ContentType","image/png");
element.put("Content","iVBORw0K...gDXgARBO/");
// Put it in the array
arr.put(element);
// Put the array and other fileds in the root JSONObject
json.put("Files", arr);
json.put("AuthToken", "XkWQ...o3UV");
// Get the JSON String
String s = json.toString();
// Get formatted and indented JSON String
String s2 = json.toString(4);
// 4 is the number of spaces to indent the string
You can use JSONObject class for this purpose
http://developer.android.com/reference/org/json/JSONObject.html