How to get values from JSON - android

I have some data in JSON format like this:
{"Datos":[{"cli_nombre":"Futcho 7","suc_nombre":"Naucalpan"}],"status":0,"mensaje":""}
The status and mensaje keys are not in Datos array. How can I get it?
I can get the values to the unique Object, this object has two keys cli_nombre and suc_nombre and I don't have problems to get the values for this keys.
Regards

If you have that JSON in a String, you could use the following code to get the status and mensaje:
JSONObject jsonObject = new JSONObject(yourJSONString);
int status = jsonObject.getInt("status");
String mesaje = jsonObject.getString("mensaje");
Hope it helps you! :)

It's not in the array but in the JSONObject.
So simply call
jObject.getInt("status");

Related

I have this type of getStringArrayListExtra but how to get exam_ques_id value form it?

arr_ques_arr = getIntent().getStringArrayListExtra("qustion_arr");
LOGLIST :
[{"user_exam":[{"exam_ques_id":"82","exam_id":"2","answer":"-1","user_ans":"0"},{"exam_ques_id":"90","exam_id":"2","answer":"-1","user_ans":"2"}]}]
use JSONObject class to parse the json

How to extract objects from json

I have a scenario where I need only 1 objects out of the entire json.
{"id":"1","first_name":"Steve","last_name":"Holt","user_type":"Teacher","user_key_area":"Math"}
In above I want to extract user_type.
How will I do?
You can use Google GSON to parse the json response and map it to the model directly . Here is the tutorial for the same TUTORIAL
Use this to extract the user_type from the json, pass the your json response in place of response variable.
JsonObject object =new JsonObject(response);
String user_type = object.getString("user_type");
You are getting this json in response you can get 1 object like this:
String value = response.getString("Key Name");
Get Response and that create JsonObject
JsonObject jsonObject =new JsonObject(res);
Create String object and pass string parameter "user_type"
String userType = jsonObject.getString("user_type");

How to Get JSONObject Only Android

{"StatusID":"1","Error":"Register Successfull."}
This my JSONObject I don't know how to convert it to string in android
Advice me Please
Thank.
Get the JSON object and get values like this.
JSONObject jsonObject = new JSONObject(res);
String statusId = jsonObject.getString("StatusID");
String error = jsonObject.getString("Error");
If you want just convert this json:
{"StatusID":"1","Error":"Register Successfull."}
you must use toString() method.
If you want get "status id" or "error" you should use getString() method
See THIS.

Trouble with Android JSONObject from Grails webservice

Okay I am quering data from a Grails webservice that returns JSON. The JSON when viewed with the JSONViewer app parses fine. When I take that same string and use JSONObject(string) in my Android app I get "value of String cannot be converted to JSONObject."
Here's my JSON string
[[{"class":"mygrails.TopTen","id":491,"ttAmount":14200000,"ttMlId":402,"ttRank":1,"ttWeekId":1108},{"class":"mygrails.MovieList","id":402,"mlApproved":1,"mlApprovedId":5,"mlMovieId":"GNOMEOAN","mlReleaseDate":"2011-03-08T07:41:45Z","mlTitle":"Gnomeo and Juliet","mlWeekId":1106}]]
Now the JSON is comes from the standard JSON conversion of a SQL data using render from the groovy file through the import grails.converters.JSON.
... //(call to render JSON in the groovy file)
def a
a = Table.findAll("from someTable as st where st.id=" params.id)
render a as JSON
...
So I am not sure what I doing wrong and why the JSON looks a little off to me. (still new to JSON)
In json if you see "[]" means its a json array and if you see "{}" it is an json object. Both of then can have the other nested inside then.
In your case the string the starts with json array.
So try something like the following
String str = "[[{"class":"mygrails.TopTen","id":491,"ttAmount":14200000,"ttMlId":402,"ttRank":1,"ttWeekId":1108},{"class":"mygrails.MovieList","id":402,"mlApproved":1,"mlApprovedId":5,"mlMovieId":"GNOMEOAN","mlReleaseDate":"2011-03-08T07:41:45Z","mlTitle":"Gnomeo and Juliet","mlWeekId":1106}]]";
JSONArray jsonArray = new JSONArray(str);
jsonArray = jsonArray.getJSONArray(0);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String class = jsonObject.getString("class"); // class will value "mygrails.TopTen"
Try to create an JSONArray from the String instead of JSONObject. I didn't test this but that should do the trick: you have two nested arrays that contain then actual data.
Check out your JSON online with http://jsonformat.com/
http://www.freeformatter.com/json-formatter.html
JSON Viewer
http://jsonviewer.stack.hu/
Paste your text in there and you can see what you should parse:

how to convert json object to string in android..?

I want to convert from JSONObject
{"CNo":80,"CName":"ganesh","CMail":"ganesh#ganesh.com","CMailType":"home","CPhNo":9878987776,"CPhNoType":"home","ClientNo":1}
to
{\"CNo\":80,\"CName\":\"ganesh\",\"CMail\":\"ganesh#ganesh.com\",\"CMailType\":\"home\",\"CPhNo\":9878987776,\"CPhNoType\":\"home\",\"ClientNo\":1}
Try to use toString() on your JSON object
I hope this helps. Just learnt it yesterday :)
JSONObject foo = yourJSONOject;
String uid = foo.get("CNo").isString().toString();
String type = foo.get("CName").isString().toString();
.
. //for each Key field.
I am not sure why you have put the escapes in the string, but you can call append() and get the OP as you want it.
Below code will convert the given JsonObject to string.
Gson gson = new Gson();
String bodyInStringFormat = gson.toJson(passYourJsonObjectHere);

Categories

Resources