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');
Related
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
I'm using com.fasterxml.jackson.databind with Retrofit to handle the response from the server in my Android app.
Since the JSONObject response is too complicated and contains lots of JSONArray, I want to be able to parse some of those array fields into String instead of creating POJO for each sub-object that those Array could contains.
Is there a way I could just tell Jackson to keep those field as String and not parse them into Entities?
In my app I have to send a big dataset back to our server for processing. I am using ksoap for all my requests to pull stuff from the server with your normal xml properties and attributes but in the one call I have to use a dataset to send information.
Is there anything in the ksoap library for android that makes this whole process easier?
basically right now I am just constructing this huge string with all these header,tags and a shcema
example:
String header = "<mmAps diffgr:id=\"mmApps"+String.valueOf(count)+"\" msdata:rowOrder=\"0\" diffgr:hasChanges=\"inserted\">\n";
String ecmmaID = "<ECMMAID>"+c.getString(c.getColumnIndex(Apparatus.APP_ECMMAID))+"</ECMMAID>\n";
etc..
String datasetToSend = header+ecmmaID+....;
and then I would make the request passing in the big string
Please tell me there is some sot of easier way to do this.
Also changing away from data sets is not a possibility since its out of my control
JSON is the best option that you can use easily with KSOAP. This would be structured and far more better than your generated string.
1. Make identical Model class in android and your server (C#.Net, Java, etc.)
// In Android
class MyData {
String someThing;
public getSomeThing() {}
//...
}
2. Encode that dataset to JSONArray in android using model class
// Create JSON Objects in loop for entire dataset
JsonObject jo = new JsonObject();
jo.add(myData.getSomthing());
// Add all JSON Objects in JSONArray
JSONArray jArray = new JSONArray();
jArray.add(jo);
3. Send this JSON as string using KSOAP
String toSendViaKsoap = jArray.toString();
4. Decode that string from json to list of model class on server.
Depending on your server language, decode that string and create objects of similar class of step 1 in native language here, and do whatever you want.
If you have .NET server application, there are lots of free libraries to dacode json inclulding builting json support as well. but I will prefer this one.
Hope this helps..:)
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
I have a question that I am a little bit confused about. I am quite new to JSON and getting JSON values in the android API. I am trying to access an array within the response I get. the JSON code I am getting is something like this:
Response:
{
"event": {
"participants": []
},
"status": "success"
}
How would I access the participants array and store their values. This is what I am trying at the moment... but I dont appear to be getting what I want.
try{
//get the JSON values from the URL.
JSONObject json = jParser.getJSONFromUrl("http://somesite.com/api/find?"+"somevar="+someJavaStringVar);
json_event = json.getJSONObject("event");
JSONArray json_array_participants = json_event.getJSONArray("participants");
} catch(JSONException e) {
}
The thing I am mostly confused about is... what is the arrays type equivalent to. Any advice or reasoning as to the correct way to get ahold of that variables value would be great... thanks guys.. :).
Think JSON is really just a key-value pairing. The JSONArray type is just an array full of objects (like Object[]) - it has no idea what the objects it contains are or what they're to be used for. Its up to you to assign meaning to the JSON stream based on what you know of the source. From what I see of your code, most of it looks fine, though I don't know what your jParser.getJSONFromURL() is doing. Typically, you would build the JSON from the response string like so:
String jsonString = getJSONFromUrl("http://somesite.com/api/find?"+"somevar="+someJavaStringVar);
JSONObject json = new JSONObject(jsonString)
JSONObject json_event = json.getJSONObject("event");
JSONArray json_array_participants = json_event.getJSONArray("participants");
You can iterate through the array like any other array to get subobjects or whatever:
for(int i=0; i < json_array_participants.getLength(); i++) {
JSONObject participant = json_array_participants.getJSONObject(i);
// Do stuff
}
As a side note - I WOULDN'T use GSON until you understand the underlying protocol, at least a little - because you never know when you might want to parse your JSON from a different language for some reason.
I would strongly recommend to use gson instead as your preferred parser since it will do all the job of serializing and deserializing for you except creating the domain objects.
This tutorial should get you going:
http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html
This will depend on what the server is supposed to return. It could be an array of anything and if this is a public service, there should be a specification to go off of.
If you are in charge of the server portion as well, and you have a backing object, Google's GSON library is extremely easy to use. It will also keep type information straight.