JSONArray weird Output on Different Devices - android

hi i have the following String
[{"SUBSCRIBERID":"151-52-049","LONG":"46.69127274","LAT":"24.70912743"},{"SUBSCRIBERID":"151-29-016","LONG":46.69570000","LAT":"24.70770000"}]
i am converting this String to JSONArray
JSONArray jsonArrr = new JSONArray(String);
then looping the Array And getting JSONObject
for (int h = 0; h < jsonArrr.length(); h++) {
jsonObj = jsonArrr.getJSONObject(h);
Iterator<String> iter = jsonObj.keys();
Object valuee = "";
String key = iter.next();
Log.e(TAG, "key is: " + key);
try {
valuee = jsonObj.get(key);
Log.e(TAG, "key: " + key + "---value: " + valuee); //on htc key is SUBSCRIBERID and its value while on samsung key is LONG and its valuee!!
} catch (JSONException e) {
Log.e(TAG,"SOME ERROR: "+e);
}
in this for loop the value retrieved is the first value in each object.
on my HTC mobile the first value is displaying correctly as the value of SUBSCRIBERID buy on a samsung device, the first value is displaying as LONG.
please any help would be appreciated!!

Order of the keys is not defined. So if you execute the code on different devices you may see keys in different orders. REF
Returns an iterator of the String names in this object. The returned iterator supports remove, which will remove the corresponding mapping from this object. If this object is modified after the iterator is returned, the iterator's behavior is undefined. The order of the keys is undefined.

Use without Iterator as mentioned in comments and above answer
for (int h = 0; h < jsonArrr.length(); h++) {
jsonObj = jsonArrr.getJSONObject(h);
// Iterator<String> iter = jsonObj.keys();
// Object valuee = "";
// String key = iter.next();
// Log.e(TAG, "key is: " + key);
// try {
// valuee = jsonObj.get(key);
// Log.e(TAG, "key: " + key + "---value: " + valuee); //on htc key is SUBSCRIBERID and its value while on samsung key is LONG and its valuee!!
// } catch (JSONException e) {
// Log.e(TAG,"SOME ERROR: "+e);
// }
// use this code
String value = jsonObj.getString("SUBSCRIBERID");
Long lat = jsonObj.getLong("LAT");
Long long = jsonObj.getLong("LONG");
Log.d("json","subscribed:"+value+",lat"+lat+",long:"+long);

for (int h = 0; h < jsonArrr.length(); h++)
{
Log.e(TAG,"JSONObjet at "+h+" : "+jsonArrr.get(h).toString());
}
try to run this

Related

Getting property of an object which has non-fixed keys

Assume that I get this JSon Response from webservice:
{
"id":13,
"name":"Alireza",
"required_id":"14",
"all_friends_id_and_name":{
"28":"Hassan",
"21":"Mohammad",
"68":"Ali",
"14":"Taha",
"96":"Darya"
}
}
In this response required_id value specifies that I need a friend name with id 14, so in all_friends_id_and_name object I should get the value of "14" (Taha) and use it in my app.
I'm using retrofit2 and gson libraries.
How to get property of an object which has non-fixed keys?
For Native approach, You can Try with Iterator .
An iterator is an object that enables a programmer to traverse a
container, particularly lists.
"all_friends_id_and_name":{
"28":"Hassan",
"21":"Mohammad",
"68":"Ali",
"14":"Taha",
"96":"Darya"
}
For the above section, Your LOGIC will be
JSONObject jsonData = new JSONObject("Json_response");
Iterator iteratorObj = jsonData .keys();
while (iteratorObj.hasNext())
{
String str_json_Key = (String)iteratorObj.next();
// Print=28,21....96
}
Here is your Json:
{
"id":13,
"name":"Alireza",
"required_id":"14",
"all_friends_id_and_name":{
"28":"Hassan",
"21":"Mohammad",
"68":"Ali",
"14":"Taha",
"96":"Darya"
}
}
Try by doing like this :
try {
JSONObject jsonObject; // it should be your above JsonObject
Iterator<?> keys = jsonObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
System.out.println("Mykey: " + key + " value: " + jsonObject.getString(key));
}
} catch (JSONException e) {
e.printStackTrace();
}

How to load JSON data into Android Listview?

I want to load JSON data into the List view with subtitle and images (Left side on the row).
Here below I have posted my sample JSON response code :
for (int i = 0; i < contacts.length(); i++) {
JSONObject obj = contacts.getJSONObject(i);
Iterator keys = obj.keys();
while(keys.hasNext()) {
// loop to get the dynamic key
String currentDynamicKey = (String)keys.next();
// store key in an arraylist which is A,B,...
// get the value of the dynamic key
JSONArray currentDynamicValue = obj.getJSONArray(currentDynamicKey);
Log.d("Response: ", "> " + currentDynamicKey);
int jsonrraySize = currentDynamicValue.length();
if(jsonrraySize > 0) {
for (int ii = 0; ii < jsonrraySize; ii++) {
JSONObject nameObj = currentDynamicValue.getJSONObject(ii);
String name = nameObj.getString("name");
System.out.print("Name = " + name);
//Log.d("Response: ", "> " + name);
//store name in an arraylist
}
}
}
}
I want to Show Tittle : currentDynamicKey values and Sub Title : Name string values.
If you already have your data and you don't necessarily need to integrate the server requests with your adapter, then you really just need an adapter which supports a JSONArray. There's a really nice open source one within the Advanced-Adapters library. Then it's just a matter of passing the JSONArray to the adapter and setting the adapter to a ListView.

data from a jsonobject in android

The below is called from a string url and returns a json object which has an json array inside, but something else is not working, can anyone show me how to access the data inside?
{"data":[{"8196":{"booking_id":"8150","client_id":"107","venue_id":null}}]
String jsonStr = "{\"data\":[{\"8196\":{\"booking_id\": \"8150\",\"client_id\": \"107\",\"venue_id\": null}}]}";
try {
JSONObject json = new JSONObject(jsonStr);
JSONArray array = json.getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
JSONObject o = array.getJSONObject(i);
Iterator it = o.keys();
while (it.hasNext()) {
String name = (String) it.next();
o = o.getJSONObject(name);
int bookingId = o.getInt("booking_id");
int clientId = o.getInt("client_id");
int venueId = o.optInt("venue_id");
Log.v("TEST", String.format("Booking ID: %s -- Client ID: %s -- Venue ID: %s", bookingId, clientId, venueId));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
I guess this is what you'll want. By the way, the JSON you posted is malformed. It's missing a } in the end.
You first need to decode the JSON string. The JSON sting you got in return is actually a serialized version of a JSON object.
You need to decode that string into a native Java Object.
There are a number of methods to this in Java. You can start by checking out the Java section at json.org.
I think the problem is that the value of key venue_id is null. You can replace null value with empty string(""), try it.

JSON parsing returning a null value

I am trying to parse the response i get from the network database. I am getting only one value i.e. seller id in response. When i try to pass it in JSON Object, somehow the toast after it doesn't execute and the toast after it is not executed.
I know there are many related questions but i can't find whats wrong... please help. Thanks.
try {
JSONArray jArray = new JSONArray(stuff.toString());
Toast.makeText(this, "len: " + jArray.length(), 1000).show(); // length is 1
for (int i = 0; i < jArray.length(); i++) {
Toast.makeText(this, "Arr: " + stuff, 1000).show();
// Arr: [{"seller_id":"5"}]
JSONObject jObject = jArray.getJSONObject(i);
j_id = jObject.getString(stuff);
// not getting executed
Toast.makeText(this, "JSON: " + j_id, 1000).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
Toast.makeText(this, "View: " + j_id, 1000).show(); // j_id is giving null value
This is my json data [{"seller_id":"5"}] i am trying to get the value of seller id (ie 5 here) out of it.
There is a problem in jObject.getString() method args
JSONObject jObject = jArray.getJSONObject(i);
j_id = jObject.getString(stuff);
Your giving the array. It should the name of the object i.e seller_id
so after the modification your code would be j_id = jObject.getString("seller_id");
After the following code:
j_id = jObject.getString(stuff);
add textview.settext(j_id);
and change this textview with your textview name
and if its not working than show your complete json data.

JSON can't read, key reading fail maybe

I wonder why I can't read the JSON Object like this :
{
"1":{"bulan":"Januari","tahun":"2012","tagihan":"205000","status":"Lunas"},
"2":{"bulan":"Februari","tahun":"2012","tagihan":"180000","status":"Lunas"},
"3":{"bulan":"Maret","tahun":"2012","tagihan":"120000","status":"Lunas"},
"4":{"bulan":"April","tahun":"2012","tagihan":"230000","status":"Lunas"},
"5":{"bulan":"Mei","tahun":"2012","tagihan":"160000","status":"Lunas"},
"6":{"bulan":"Juni","tahun":"2012","tagihan":"150000","status":"Belum Lunas"},
"panjang":6
}
with my android code like this :
try {
int length = jobj.getInt("panjang");
for(int n = 0; n < length; n++){
String m = Integer.toString(n)
JSONObject row = jobj.getJSONObject(m);
String bulan = row.getString("bulan");
String tahun = row.getString("tahun");
String tagihan = row.getString("tagihan");
String status = row.getString("status");
HashMap<String, String> map = new HashMap<String, String>();
map.put("bulan", bulan);
map.put("tahun", tahun);
map.put("tagihan", tagihan);
map.put("status", status);
list.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
It always return nothing, but it works fine if I change the key m to specific key like if
String m = "1";
and I can't use
JSONObject row = jobj.getJSONObject(n);
because getJSONObject() just accept string, not int.
is there something wrong with my code?
Your problem lies in the initial iterator value. It failed to search for key "0", because you don't have "0". Change it to:
for(int n = 1; n <= length; n++){
Should fix the problem.

Categories

Resources