Well I'm new to Android. I'm getting a JSON string from a remote URL.
[{"key":"myString1","val":"myValue1"},{"key":"myString2","val":"myValue2"},{"key":"myString3","val":"myValue3"},{"key":"myString4","val":"myValue4"},{"key":"myString5","val":"myValue5"}]
I just need to parse this JSON string & display all key-val pair. I tried something like below from one of the tutorial.
JSONArray jArray = new JSONArray(str);
json = jArray.getJSONObject(0); //This will take first pair.
But I don't know the syntax for iterating through whole json object. Any help would be appreciated. Thanks in Advance.
There's nothing special in it. You do it like iterating any other array.
Let's say you have two String arrays to be filled with values: String[] mKey, mValue
Reading from JSON array will be like:
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
mKey[i] = object.getString("key");
mValue[i] = object.getString("val");
}
Related
This may be possible of duplicate question but am struggling with this am getting json array response like this:
[{"data":"25"},{"MobID":"88"}]
JsonArray jsonarray=new JsonArray(serverresponse);
for(int i=0;i<jsonarray.length();i++){
JsonObject json=new JsonObject(i);
String data=json.getInt("data");
String Mobid=jsong.getInt("MobID");
}
}
Is it possible to parse this type of json i haven't found any parsing method for this above method as a beginner am struggling with this you people are here to help beginner like this Thanks in advance!!!
Try this:
JSONArray array = new JSONArray(serverResponse);
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
int data = object.getInt("data");
int mobid = object.getInt("MobID");
// use them ...
}
Hint: the secret (sh!) is to read the javadocs! All of the methods you needed are in the JSONArray and JSONObject javadocs.
Note: I corrected a number of style errors in your code (and some bugs). Please compare your version and mine to see what I fixed.
I have an JSON something like:
{
"store":"usa",
"values":["1","2","3","4"];
}
and so I go ahead and get a JSON object for values:
JSONObject values = json.getJSONObject("values");
So now I have this Json object but methods like getName("X") does not work for this type of JSON. There is no key value for the Array now. Its just Strings oen after another.
I want it to return like
String[] listValues = value.getArray();
But I don't see anything like this.
Any ideas ?
Thanks !!
Have you tried this?
JSONArray a = json.getJSONArray("values");
for (int i = 0; i < a.size(); i++) {
Log.d("Type", a.getString(i););
}
String myJson = "{
"store":"usa",
"values":["1","2","3","4"]
}";
JSONObject jsonObject = new JSONObject(myJson);
JSONArray array = jsonObject.getJSONArray("values");
I'm having a provblem on creating a JSON object on android app.
I's getting a JSON array like :
[{"estado":"1","longitude":"19.1714172","latitude":"-8.9477729"},{"estado":"1","longitude":"37.1714681","latitude":"-8.9476652"}]
from the server (using a servlet).
But when i try to access the array objects on a for cicle like:
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
//the rest of code
}
my app gives some error, and simple stop (something like an ANR! i'm not sure about this).
Can anyone help? please.
First, please remove the exclamation mark "!" at the end of your sentences, it might offend somebody here ^^
Okay, onto your problem, I suggest to encapsulate your JSON array inside a json object, maybe like this :
{"data":[{"estado":"1","longitude":"19.1714172","latitude":"-8.9477729"},{"estado":"1","longitude":"37.1714681","latitude":"-8.9476652"}]}
For example, we save that result string inside a String called jsontext, then we would create a JSONObject from that string, grab the JSONArray, and parse the JSONObject on the array. Here's a little snippet :
String jsontext = "yourjsontext";
//if it's from a http response, you might call respon
JSONObject mainObject = new JSONObject("jsontext");
JSONArray jsonArray = mainObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
//the rest of code
} code here
Hope this helps, good luck ^^
Regards,
Reid
i am trying to access the json data from server, I have accessed the first json array from file but I don't know how to access the another json array from that file. Any help would be appreciated.
my url of json file is as follows
String jsonurl" = "http:// 66.7.207.5 /homebites/list_business_category.php?b_id=18";"
I have accessed json array "business" as follows,
json_object_main = json_parser.getJSONObjectFromUrl(jsonurl
+ res_id);
Log.i("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", json_object_main + "");
if (json_object_main != null) {
try {
JSONArray json_array_header = json_object_main
.getJSONArray("business");
JSONArray j=json_object_main.getJSONArray("business_cat");
//JSONArray json_cat=json_parser.getJsonArayFromUrl(jsonurl+res_id);
now I want to access json array "business_cat"
how can I do that?
I think you are looking for this:
JSONArray j=json_object_main.getJSONArray("business_cat");
for(int i=0; i < j.length(); i++)
{
JSONObject jsonAttribs = j.getJSONObject(i);
//do whatever you want to do with the elements
jsonAttribs.getString("SOME_KEY");
}
Hope it helps!
i need to parse the JSON data given below.
{"result":[{"bookId":142645,"bookpb":"MF",
"bookTs":1328999630000,"clipStatus":"D","bookDetail":{"arrival":1,"purchase":1,"sold":1},
"hierarchies":{"categories":["4"],"events":[]},"shopId":769752},
upto "sold" it is working fine.but when i am trying to parse categories it is not working.
given below is the code for parsing the data.
ArrayList<BookItem> resultdata = new ArrayList<BookItem>();
JSONArray jsonArray = (new JSONObject(inputString))
.getJSONArray("result");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
item = new BookItem();
item.setbookId(jsonObject.optString(book_ID));
item.setPurchase(jsonObject.optInt(PURCHASE));
item.setArrival(jsonObject.optInt(ARRIVAL));
item.setSold(jsonObject.optInt(SOLD));
item.setbookTs(jsonObject.optString(book_TS));
JSONObject hierarchies=jsonObject.getJSONObject(HIERARCHY);
item.setCategory(hierarchies.getInt("categories"));
resultdata.add(item);
}
can anybody help me???
i came to know that this is the problem of
{"categories":["4"],"events":[]}
data.how can i parse this array value?
categories is an JSONArray in order to get JSONArray
replace
item.setCategory(hierarchies.getInt("categories"));
with
item.setCategory(hierarchies.getJSONArray("categories").getInt(0));
For your purpose the best solution is probably GSON library. It do serialization and deserialization on its own and you will get your objects.
http://code.google.com/p/google-gson/