{"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");
}
Related
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.
I have tried several of the examples listed and none of them seem to work for some reason so I am posting a new question.
I have a JSON string coming to me however it is all objects (it is not formatted in an array). I need to parse out the Team names from this. I know I need to iterate over them as they are the keys and then store the objects in a new array.
Here is the JSON (a sample of it)
{"query":{"printrequests":[{"label":"","typeid":"_wpg","mode":2,"format":false}],"results":{"Team:Kubbchucks":{"printouts":[],"fulltext":"Team:Kubbchucks","fullurl":"http://wiki.planetkubb.com/wiki/Team:Kubbchucks","namespace":822,"exists":true},"Team:Kubbchucks
IDP":{"printouts":[],"fulltext":"Team:Kubbchucks
IDP","fullurl":"http://wiki.planetkubb.com/wiki/Team:Kubbchucks_IDP","namespace":822,"exists":true}},"serializer":"SMW\Serializers\QueryResultSerializer","version":0.5,"meta":{"hash":"8407a177d701d746edc3066a012c17d2","count":2,"offset":0}}}
What I have so far to dig down to that level of the JSON (data is the string above)
JSONObject parsing = new JSONObject(data);
JSONObject query = parsing.getJSONObject("query");
JSONObject results = query.getJSONObject("results");
This allows me to manually check for a team using :
JSONObject teamone = results.getJSONObject("Team:Kubbchucks");
This is of course not ideal. The full JSON file has almost 3000 entries and I do not know what those entries all are. I need to iterate over the results object to get the keys that start with "Team:"
I've tried a few different iterator samples but none seem to be working for me and it is starting to really frustrate me. Believe me, I've tried many things before asking the question. Any help is appreciated.
I had a similar problem some time ago.
You can do like this:
JSONObject results = query.getJSONObject("results");
Iterator<String> iterator = results.keys();
while (iterator.hasNext()) {
JSONObject team = results.getJSONObject(iterator.next());
// do stuff
}
I know how to get jsonarray from jsonobject. I am doing like below code to get jsonarray.
JSONObject recvJson = new JSONObject(holder.toString());
String numberByte= (String) recvJson.get("data");
String ts = (String) recvJson.get("time");
JSONObject temp2 = new JSONObject("{ \"data\" : " + numberByte+ "}");
JSONArray recvJarray = temp2.getJSONArray("data");
for (int i = 0; i < recvJarray.length(); i++)
{
byteArray[i] = (byte) recvJarray.getInt(i);
}
But don't want to use for-loop, without using for-loop or any other loop want to retrieve jsonarray data values.
How should I do ?? I have done google & saw many forums but dint succeed to retrieve data without using for-loop.
for example : I ll be getting 80 - 100 packets of 1024bytes per second from server, I want to retrieve this data & store it into bytearray. By usingf for-loop its taking around 300ms- 400ms and I am loosing many packets between that. So I want to use different approach. If any Idea or solution for cracking this.
Help will be appreciated !!
You can use GSON to parse the json objects. It is much faster and easier to decode json data.
Check these links.
Gson1, Gson2
try usig gson
it uses java reflection to convert objects to json and json to object with simple methods
toJson(),fromJson() (and it works for object arrays and lists too)
but you have to write the proper classes for the jsons (with all fields)
read the documentation.
I have not been working with JSON very long. I have the following response and I'm struggling to understand how to call the details of the "Topic". I thought it was a JSONObject, but Android's logcat is telling me JSONObject["Topic"] not found.
[{"Id":1,"TopicId":1,"UserGuid":"C214ED74-07A7-409E-84FF-AF0457CF581A","Topic":{"Id":1,"AdminUserGuid":"C214ED74-07A7-409E-84FF-AF0457CF581A","Title":"Test Topic 1","AccessType":"public"}}]
Any help is greatly appreciated.
This is a JSONArray with one element, a JSONObject with multiple properties, one of which is a JSONObject named topic:
JSONArray posts = new JSONArray(myJsonString);
JSONObject post = posts.getJSONObject(0);
JSONObject topic = post.getJSONObject("Topic");
Use JSONLint to indent your JSON string this way you can understand the structure better
It's a JSONArray with one object in which there are 4 objects and one of them has 4 attributes.
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.