I use below code :
JSONObject value = ...;
JSONObject stationList = value.getJSONObject("stationList");
Iterator<String> iter = array.keys();
while (iter.hasNext()) {
try {
String key = iter.next();
String val = array.get(key).toString();
Log.i("res", key+"- "+val);
} catch (JSONException e) {
e.printStackTrace();
}
}
My data sort is :
1- name
3- other
2- your
But output is :
1- name
2- your
3- other
I think JSONObject reorder data with integer key.
How I can fix it and read data without reorder?
JSONObjects are similar to map which stores data as a key value pair without maintaining order.
Related
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();
}
According to this json file I want to receive data1 , xyz, abc object and their sub objects and show it in recyclerview.But I am receiving only data1 and their key-value pairs(index1 and name). I am using library Volley.
Can someone help me how to recive xyz and abc ?
{
"Data1":{
"index1":"4",
"name":"dan"
},
"xyz":{
"index1":"2",
"name":"jimi"
}
"abc":{
"index1":"5",
"name":"jordan"
}
}
JSONObject jsonObject = response.getJSONObject("Data1");
getset1.seti(jsonObject.getString("index1"));
getset1.setn(jsonObject.getString("name"));
Try this
JSONObject jsonobject=new JSONobject("");
for (int i=0;i<jsonobject.length();i++){
JSONObject jsonObject1=jsonobject.getJSONObject(i);
);
You have to use loop.As this is not an array so u have to fetch the all keys using iterator and then loop among them try this
Iterator<String> keys = json.keys();
while (keys.hasNext())
{
// Get the key
String key = keys.next();
// Get the value
JSONObject value = json.getJSONObject(key);
// parse inner string from value
}
I'd like to retrieve the value of each key from my json file cardInfo
"cardInfo" : {
"-KF3fzOc3e68jRpCkLRb" : 6869098993309203,
"-KF3g0fQUJHcexwAFzxz" : 6764366404306628,
"-KF3hHOdj-siBxLMNgLa" : 4368998299921475,
"-KF3hLzxQsmTP9MJ6iVe" : 8779395075647930,
"-KF3hM3_mOn_VoN9-bW6" : 6867202628499185,}
But since i'm using Push method for saving these random numbers , which method shall i use to get access to those values through these random keys to save them into some TextViews.
Thanks :)
You can use public Iterator<String> keys()
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.
Example:
JSONObject jObj = new JSONObject(response);
JSONObject json = jObj.getJSONObject("cardInfo");
Iterator<String> iter = json.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
Object value = json.get(key);
} catch (JSONException e) {
Log.e("JSON-ERROR",e.toString());
}
}
I am having JSON object like
{
key1:value1,
key2:value2
key3:value3
}
and i will write this JSON content to file.(Done)
For next interval of time i am getting JSON Object as
{
key1:newValue1,
key2:value2
key3:newValue3
}
I need to find out difference between each values. and need to write new json into file
{
key1:(value1- newValue1),
key2:(value2 - value2)
key3:(value3- newValue3)
}
How can i achieve it.? Please help.
The code below just iterates through the keys of your testObject, subtracts the value of the object from file and puts the result of this subtraction back to the testObject... Than you can save, or do whatever you want to do with the values in testObject
for(Iterator<String> iterator = testObject.keys(); iterator.hasNext();) {
String key = iterator.next();
try {
int testValue = testObject.getInt(key);
int fileValue = fileObject.getInt(key);
int differenceValue = testValue - fileValue;
testObject.put(key, differenceValue);
} catch (JSONException e) {e.printStackTrace();}
}
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.