I want to parse JSONObject and get its keys in the order they do represent to me when i receive. I don't want use JSONObject.keys() as this function giving undefined order of json keys and fully unordered,and this is a huge problem right now because I'm stuck in this position and i have to get json object keys.So is there any way to achieve it? Doing researches i never came across to code which can parse json object keys except JSONObject.keys() which giving reverse order.
There is no way to achieve what you want with a JSONObject because JSONObjects are not ordered by definition and you shouldn't rely on the insertion order. If you need something ordered you should look into JSONArray
I have noticed that although JSONArray gives the proper order, the order won't be kept in HashMap, if you're using it for saving the values.
Using LinkedHashMap solves the order issue.
Related
I had error
org.json.JSONException: No value for data
image
I had tried many ways but I can't solve it. Please help me to fix it.
It is possible to use Gson API to parse JsonObject above to java Object?
You need to get posts before.
JSONArray data = responde.getJSONObject("posts").getJSONArray("data");
instead of
JSONArray data = responde.getJSONObject().getJSONArray("data");
This should work for this scenario, but you really need to check for non existent keys or values.
Im doing a dynamic form to Android and I need just read a Json file and build a form and it should work in anyway. How can I do it? In "name" getString("name") what I can put there to get a name without write "name" inside de method?
Is the question that you want to find the keys of a JSONArray? Iterate through the array, grab the key of each object. Then, grab the value.
See second answer: Retrieving Keys from JSON Array key-value pair dynamically - Javascript
I think the following should help:
http://developer.android.com/reference/org/json/JSONObject.html#keys()
You can use this Iterator to iterate through all the keys and then use the key to extract the desired values.
Additionally, if dealing with a JSONArray you could use the following too:
http://developer.android.com/reference/org/json/JSONObject.html#names()
I am using PHP as a middleman to access a MySql database and it returns the result of the query as a json string using json_encode, then display it within the TableLayout of the app, this is why order is important so I can line up the data and the headers.
After some research I found out that json does not enforce order so any time I call new JSONArray(result) it scrambles the json returned by PHP. Is there any way to preserve the order of the returned string? Or maybe I'm using the incorrect data structure on either end.
Relevant PHP result:
[{"FIELD1":"vsa","FIELD2":"dfs","FIELD3":"dsfa","FIELD4":"adsf","FIELD5":"23","ZIPCODE":"asdf","USERNAME":"asd","PASSWORD":"as","DATE1":"dsfa"}]
Relevant Android Result After JSONArray(result):
[{"ZIPCODE":"asdf","DATE1":"dsfa","FIELD3":"dsfa","FIELD2":"dfs","FIELD5":"23","FIELD4":"adsf","USERNAME":"asd","FIELD1":"vsa","PASSWORD":"as"}]
I believe the reordering inside a JSON object is due to the fact that JSON objects are key/value pairs (not an indexed array), which by default are unordered. However, the JSON array is an ordered sequence of values (JSON objects).
Don't rely on order!
Source: http://www.ietf.org/rfc/rfc4627.txt
I've never seen new JSONArray(String) change the order of anything, and I've used it a lot. However, what you have seems to be an array of length 1. Using myJsonArray.getJsonObject(0).getString("ZIPCODE") should still return the correct data, and as long as you query in the correct order (FIELD1, FIELD2, FIELD3, etc), you should be fine.
I am currently using a custom service in my android application to retrieve a JSONArray from a remote database. Once I have retrieved the JSONArray, I would like to compare its contents to another JSONArray which I call 'mostRecent.' If the newly retrieved JSONArray differs from 'mostRecent' then I would like to update 'mostRecent' with the newly retrieved JSONArray. If I am not mistaken, however, the JSONArray equals method compares the instance of the JSONArray object and not the contents, correct? Is there an easy method to compare the contents of two JSONArrays? Thanks for the help!
Use that:
array1.toString().equals(array2.toString());
Maybe you will be able to use even the strings you receive without constructing the JSONArrays, but then you will need to make sure the two are formatted the same way.
Suppose I have a json string like this:
{ ... "key1":"value1"; ... }
with a key1-value1 pair somewhere deep down the json structure (which includes other things such as array, dictionary, etc...). I don't know exactly (and don't care) how the exact structure of the json is.
Is there a simple way to extract the "value1" ? (if there are 2 "key1" in the json string then I just need the first one).
As far as I know, you have no chance of doing it manually.
If you really don't know what's the structure of the JSON string you're expecting, you can try a graph search approach, such as DFS (http://en.wikipedia.org/wiki/Depth-first_search).
For every key, check if it is an array.
If so, go inside and repeat the procedure. If nothing was found in a given array, backtrack.
Interrupt your process once you have found your key.