Trouble on creating a JSON Object on Android - android

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

Related

How to parse Multiple json array in android?

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.

Parse JSON string from remote URL in Android

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");
}

Android Json parsing how to get Array

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");

Android : parse the json within json object in java

I have the json like this..
{"source":[{"id":"22","name":"xyz"},{"id":"23","name":"ghj"},
{"id":"24","name":"tuv"}]}
I've to get the value of "id" and "name". How to do that..
I've tried a lot. But can't get the best solution how to do this.
thanks for the help
You want to use following code....
JSONObject jObject = new JSONObject(responseBody);
sourceArray = jObject.getJSONArray("source");
for (int i = 0; i < 3; i++) {
name[i] = sourceArray.getJSONObject(i).getString("name").toString();
id[i] = sourceArray.getJSONObject(i).getInt("id");
}
responseBody is your actual JSON String.
Reply if you have any doubts.

how to parse JSON string with value as array

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/

Categories

Resources