public JSONObject (String json) in api19 scrambles order? - android

I was parsing a string to a JSONObject and noticed some weird behaviour. The orders of items get scrambled? Is this a bug or did i do something wrong?

Your 'items' are key/value pairs in a JSON object.
The order of key/value pairs within a JSON object is not guaranteed:
An object is an unordered set of name/value pairs.
If you need to preserve the order of those items you can put them in an array on the serializing side (probably a server):
An ordered list of values. In most languages, this is realized as an
array, vector, list, or sequence.
Official docs.

Related

Creating json object without keys from array in android

Is there any way to create json object with only values and no keys, from array list in android?
I have array list like this
[12,name,description]
But i need to convert this to JSON Object like this
{"12","name","description"}
How to do this? Can anyone help?
No, you cannot create JSONObject without its key.
A modifiable set of name/value mappings. Names are unique, non-null
strings.
Source
Definition itself says names are unique, non-nullso you need to use key-value pair only, however it it possible to create JSONArray like this, but in that case also that JSONArray have some key.
"someKey":["a","b","c"]
So in this case also JSONArray needs to define under some key.

Get JSON Object Keys in a loop with default order android

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.

JSON array order

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.

multiple json string in android

I have to return all my JSON string. For example I have one json string:
[{"Locationvalue":"Payroll - 9","LocationId":"465","IsSelected":false}]
and also returned second JSON string:
[{"CC2Description":"Denver - DN","CC2":"DN","isSelected":false},{"CC2Description":"Las Vegas - LV","CC2":"LV","isSelected":false}]
ans so on.
In android I have written this:
JSONArray JsonObject = new JSONArray(JsonString.toString());
for(int i=0;i<JsonObject.length();i++)
{
Log.v("log", JsonObject.getString(i));
}
but I can only access one JSON array. I want other JSON array also.
You cannot decode multiple separate json structures in a single call. A JSON structure must be a complete proper Javascript object or array on its own, e.g.
Two arrays like this:
[1,2,3][4,5,6]
is invalid, because it's two separate arrays smashed up against each other. However,
[[1,2,3],[4,5,6]]
is ok, because it's a single array that contains two separate child arrays. You can return multiple separate json strings, but they must be contained within a single structure.

Android: Convert web service json output into java hashmap

I have the following output, where wsResponse.get("result").toString()
= {"status":1,"result":{"2011":102003,"2010":100003,"2009":98723,"2008":129023}}
I'll like to create a hashmap where the years are the key.
How will i be able to populate the hashmap?
Is it necessary to have a hashmap? Android has the org.json library, so you can do something like:
JSONObject json = new JSONObject(wsResponse.get("result").toString());
If you check out the reference, you can use JSONObject just like a hashmap. You can call get(String key) to get the value for a year.
If you must have a hashmap, you can iterate through the JSONObject and put the keys and values in the hashmap.
Take a look at JSONObject.

Categories

Resources