Json.put() function puts double quotes automatically - android

I am preparing a json object to be sent over a webservice , I am trying to put a list of String in the object, Something like this:
["24348f08-92f4-481a-9a36-ed0d533ca4f3", "24348f08-92f4-481a-9a36-ed0d533ca4f3"]
What i have done:
sendData.put("SpecializationAlert",Specialization);
sendData is a json object and Specialization is a String array, the result when i log this is:
"[\"24348f08-92f4-481a-9a36-ed0d533ca4f3\",\"24348f08-92f4-481a-9a36-ed0d533ca4f3\"]"

Specialization is put in the JSON as a toString()-ed object. You may create a JSONArray from it first and then include it in the JSONObject:
sendData.put("SpecializationAlert",new JSONArray(Arrays.asList(Specialization)));

Related

How to parse a JSON which contains an array with only square brackets using Gson?

This is my JSON response:
{
"email":[
"This field must be unique."
]
}
I want to retrieve the value of json object email and display it. I tried using Gson but always getting null. Created a model class with email variable with type JSONArray -- still no luck.
Thanks in advance .
Note that the email attribute is actually an array of strings.
The object to be deserialized by Gson should look like the following:
public class Response {
#SerializedName("email")
public List<String> emails;
}
Then using gson
Response response = gson.fromJson(json, Response.class);
And then you can access to that object by doing
response.emails.get(0)

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");

Convert Object Array to JSON String on Android

I can't seem to find this anywhere, but tons of people have to be doing this.
I have an array of objects that I want to convert to a JSON String and post to a REST URL. Here's what I have so far:
if(history==null||history.length == 0){
return new String[0];
}
JSONArray array = new JSONArray();
for(DeviceHistory connectHistory:history){
array.put(connectHistory);
}
JSONObject response = jsonClient.remoteCall(SERVICE_NAME, array.toString());
The problem is that I get ["com.abc.model.connect.DeviceHistory#41e63298","com.abc.model.connect.DeviceHistory#41e63760","com.abc.model.connect.DeviceHistory#41e63c28","com.abc.model.connect.DeviceHistory#41e640f0"] from array.toString(). What am I doing wrong?
Because this is the result of Object.toString(). You may want to try:
https://code.google.com/p/google-gson/
This library allows to convert Object to JSON and back.
Your problem is that you are not passing your object as a String, so what you are writing in your JSON is a reference to your object.
You should implement your toString() method in that class if you can or just use it. However, if you cant you will need a helper method to achieve it.

How to get a JSON with dollar sign

suppose I have {"data": {"243232": {"id": "testid","name": "test" } }}
so how to get correct value thanks.
you cant directly pass above String with Gson as "243232" is number and we cant declare variable which start from number, So for parsing this you must modify the string with some notification
i.e. {"data": {"temp243232": {"id": "testid","name": "test" } }}
here i modified string manually "243232" by "temp243232", now you can parse it
You can use a JSON parser for Java like Google GSON. This works best if the class whose object your data in json represents, is known to you.
String str = ""; //contains your json string
Gson gson = new Gson();
MyClass var = gson.fromJson(str,MyClass.class);
where MyClass will be a class which represents the object.
Then you can access data just like you'd do from a data structure in your program, it'll be something like var.data.

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:

Categories

Resources