Create a Gson/Json object by a String - android

I'm trying to convert this string :
{"status":200,"message":"OK","response":{"result":1,"time":0.29773306846619,"values":[]}}
to a Json OBJ, to be able to read values better, I read somewhere that I can use Gson library, but I don't know how.

You don't need to use GSON. Android has JSON parsing builtin. Just pass your string to the constructor.
JSONObject jObject = new JSONObject(myString);
Here are the docs for JSONObject.

Related

How to parse single object from json

I am working on an Android project where I am using volley to parse the data from JSON. I am able to parse the data if it is an array by following this tutorial. But when I try to parse a single object using getJSONObject, it is returning null. I want to get the value of that particular object.
This is my JSON file:
In the above file I want to retrieve only responseInfo which is a JSON object.
You need to create JSONObject
JSONObject object = new JSONObject(responce);
//get responce code
String query = object.getString("responceCode");
Please use Gson library for json parsing
https://github.com/google/gson

How do I turn a JsonElement into Json without the members tag?

I have a method that parses the differences between two JsonElements and then returns a JsonElement that represents an object containing only the differences between the two elements.
I am then attempting to send the returned JsonElement via API PUT request and generically parse the response to an object type. The problem is that JsonElements parse to Json with the members tag in front of all of its contents.
For Example:
{"members":{"id":1172327,"assets":[{"id":436379,"licenseState":"MI","odometer":"12345"}]}}
How can I remove the members tag from the JsonElement. I would prefer not to do it manually. I tried JsonElement.getAsJsonObject() and that fails. I also attempted to get the JsonElement as a JsonArray (JsonElement.getAsJsonArray().get(0)) but this failed because the JsonElement is not a JsonArray.
import org.json.JSONObject;
/** Assuming json_string is your json string **/
JSONObject jsonObj = new JSONObject(json_string);
JSONObject jsonMembers = jsonObj.getJSONObject("members");
//will return "{"id":1172327,"assets":[{"id":436379,"licenseState":"MI","odometer":"12345"}]}"
Log.d("Members only:",String.valueOf(jsonMembers));
Note: Make sure to surround with try/catch in case it's not valid json!!
Hopefully this is what you're looking for.
What if you just do JsonElement.getJSONObject("members") ? That will grab the element with the key "members"

JSON Array or Object Error in Android

I have a string being returned from a HttpClient called data.
Data = {"result":[{"id":"2","contextID":"1","name":"Kitchen","image":"81"},
{"id":"1","contextID":"1","name":"Living Room","image":"18"},
{"id":"3","contextID":"1","name":"Toilet","image":"75"}]}
I am then performing this code:
resultArray = new JSONArray (data);
and returning this JSONArray. However, I get a JSONException error:
JSONObject cannot be converted to JSONArray
Surely this is a JSONArray not a JSONObject? Or is it a JSONObject of JSONObjects? I'm pretty new to JSON and I'm wanting to loop through and create new Locations using these imported values. Is there an easy or established way of doing this?
Many Thanks.
Data is a JSONObject, and Data["result"] is a JSONArray that contains JSONObjects.
Its a json object with a JSONArray of JSONObjects inside of it. { } means object and [] means array. So you get the top level string as a JSON object, then get the results parameter as an array, then get each index into the results as an object (and you can get the parameters of those via getString, etc).
Data is actually a JSONObject that contains a JSONArray named "result." If you wanted to get the JSONArray you'd have to do the following:
JSONObject dataObj = new JSONObject(data);
JSONArray dataArr = dataObj.getJSONArray("result");
For future reference, since you're new to JSON, data inside {} braces is a JSONObject and data inside [] braces is a JSONArray. Arrays and objects can be nested inside of each other and it's sometimes hard to read. I recommend formatting your data if you need help reading it. I personally use http://jsonformatter.curiousconcept.com/ to format and validate my data. I'm not associated with the site in any way. I just find it really useful.

convert json string to POJO JAKSON

I am using JAKSON in my Android Project. Now i have a JSON Object in String format. How can i convert this String into a JAVA POJO class.
There are several different ways to archive this:
1) Use org.json.JSONObject, mannually parse data and put it in POJO.
2) Use third party library like GSON and decode your JSON string directly to Object. GSON for example, use annotations to define serrialization/deserrialization rules for Objects/JSONs.
With Jackson:
MyThing thing = new ObjectMapper().readValue(jsonString, MyThing.class);
For an introduction to using Jackson, I recommend taking a look at http://wiki.fasterxml.com/JacksonInFiveMinutes.
ObjectMapper mapper = new ObjectMapper();
PojoClassName Obj = mapper.readValue(JsonString, PojoClassName.class);
P.S JsonString is the String which contains Json and is to be converted to POJO

Parsing a JsonObject

I have this Json model and I want to parse only one object:
{"CodeMsg": "Server Local Time", "server_time": "2012-03-19 19:59:30", "CodeResult": "OK"}
How can I do that?
There are a number of libraries available to parse JSON. Two that are commonly used are:
Gson - http://code.google.com/p/google-gson/
Jackson - http://jackson.codehaus.org/
With both you do this:
Create a plain java object to represent your data - e.g. a class CodeMsg
Use the library to provide the JSON string/stream, and the type (CodeMsg) and an object of that type is created, with its members set according to the JSON (e.g. server_time, CodeResult, etc)
They are very easy to use.
var data = {"CodeMsg": "Server Local Time", "server_time": "2012-03-19 19:59:30", "CodeResult": "OK"};
JSONObject obj1 = new JSONObject(data);
and then use obj1.getJSONString('CodeMsg');

Categories

Resources