Converting arraylist of custom class to JSONArray - android

I didn't expect this is that complex. But I can't figure out how I can convert my arraylist of custom class to a JSONArray. I understand there is no direct method to do that. So I made arraylist of JSONObjects from my arraylist. Now in order to save this into a JSON file, how can I convert the arraylist of JSONObjects to JSONArray?

Like this:
JSONArray toReturn = new JSONArray();
for(JSONObject object : yourJSONArrayList){
toReturn.put(object);
}
toReturn will then be a JSONArray that has all the JSONObjects from your arraylist of JSONObjects in it.

Give Gson Library a try, all that custom json conversion code goes away.

Related

Json Object: linked list?

I have a Json string:
String json = "{\"I\":0,\"lst\":[{\"i1\":100500,\"s1\":\"abrakadabra\",
\"aList\":[{\"text\":\"secret will of my Dad\"}]}]}";
JSONObject o = new JSONObject(json);
My question: how, using Json Obj methods, to browse through each node element recursively?
Vitali, I don't have enough reputation points to reply to your comment, so posting it as an answer. In that post I linked, I meant the code snippet with loopThroughJson() method. I haven't tried it myself but that looks right. For completeness, this is the link again -
Recursively parsing JSON via JSONObject to fetch value against specific keys
Loop through the object, get child as reference of Object class using the get() method, if that object is instance of JSONObject or JSONArray, go deeper.

Extracting data from JSON array?

{"result":[{"FullName":"Upasiri"}]}
This is what i get when i run my php. I tried doing various things like put this ti a JSONObject and use get methods, but nothing works :/ How can i extract the "Upasiri" from that? Im new to android, so any help is very much appreciated!
Generate POJO classes with http://www.jsonschema2pojo.org/ (for example), then you can use any JSON serializer to get the Java Objects and get FullName from it. Example how to do it with a GSON you can find here - http://www.javacreed.com/simple-gson-example/
This is your answer
JSonArray array = object.getJSonArray ("result");
for (int i=0; i<array.lenght(); i++) {
JSONObject object= array.getJSONObject(i);
// this is your uripase
String url = object.getString("FullName");
}

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.

Android: Convert web service json output into java hashmap

I have the following output, where wsResponse.get("result").toString()
= {"status":1,"result":{"2011":102003,"2010":100003,"2009":98723,"2008":129023}}
I'll like to create a hashmap where the years are the key.
How will i be able to populate the hashmap?
Is it necessary to have a hashmap? Android has the org.json library, so you can do something like:
JSONObject json = new JSONObject(wsResponse.get("result").toString());
If you check out the reference, you can use JSONObject just like a hashmap. You can call get(String key) to get the value for a year.
If you must have a hashmap, you can iterate through the JSONObject and put the keys and values in the hashmap.
Take a look at JSONObject.

how to create a Json array of strings in android

I am trying to create an array like this ["stringone","stringtwo"] and pass it to the webserver. When I tried doing making it with a string array like String[]={"stringone","stringtwo"] it passed in something weird {"userids":"[Ljava.lang.String;#406fe4b8"} how should I be constructing my JSON array if not by using string arrays?
Thanks
If you want to create JSONArray from List or array, you can use constructor, which takes Collection:
String[] data = {"stringone", "stringtwo"};
JSONArray json = new JSONArray(Arrays.asList(data));
The easiest way is to create a JSONArray object and use the put method(s) to add any Strings you want. To output the result, just use the toString() method.

Categories

Resources