Reputation: 3770
I have the following string to json code:
json = new JSONObject(
"{\"Questions\":{"
+ "\"1\":[\"ELSYS\",\"ELSYS2\",\"ELSYS3\",\"ELSYS4\"],"
+ "\"2\":[\"1024\",\"2048\",\"512\",\"64\"],"
+ "\"3\":[\"RANDOM1\",\"RANDOM2\",\"RANDOM3\",\"RANDOM4\"],"
+ "\"4\":[\"A DOG1\",\"A DOG2\",\"A DOG3\",\"A DOG4\"],"
+ "\"5\":[\"SOME WORD1\",\"SOME WORD2\",\"SOME WORD3\",\"SOME WORD4\"]}}");
Right after the initialization I print the JSON to the LogCat:
Log.i(TAG, json.toString());
My problem is that the json is meshed up. This is the output:
{"Questions":{"3":["RANDOM1","RANDOM2","RANDOM3","RANDOM4"],"2":["1024","2048","512","64"],"1":["ELSYS","ELSYS2","ELSYS3","ELSYS4"],"5":["SOME WORD1","SOME WORD2","SOME WORD3","SOME WORD4"],"4":["A DOG1","A DOG2","A DOG3","A DOG4"]}}
And to be more readable here is the parsed info from the json:
key => 3 || value => RANDOM1,RANDOM2,RANDOM3,RANDOM4
key => 2 || value => 1024,2048,512,64
key => 1 || value => ELSYS,ELSYS2,ELSYS3,ELSYS4
key => 5 || value => SOME WORD1,SOME WORD2,SOME WORD3,SOME WORD4
key => 4 || value => A DOG1,A DOG2,A DOG3,A DOG4
As you can see the strings are not in the right order. I tried the same code in a normal Java program and it worked properly. Whe I do this in android the problem is there. Do you know what could be the problem? (I tried cleaning the project)
UPDATE
Trying "[" instead of "{". The following exception is thrown:
10-02 18:49:09.985: W/System.err(2263): org.json.JSONException: Unterminated array at character 18 of {"Questions":["1":["ELSYS","ELSYS2","ELSYS3","ELSYS4"],"2":["1024","2048","512","64"],"3":["RANDOM1","RANDOM2","RANDOM3","RANDOM4"],"4":["A DOG1","A DOG2","A DOG3","A DOG4"],"5":["SOME WORD1","SOME WORD2","SOME WORD3","SOME WORD4"]]}
10-02 18:49:09.985: W/System.err(2263): at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
10-02 18:49:09.985: W/System.err(2263): at org.json.JSONTokener.readArray(JSONTokener.java:440)
10-02 18:49:09.985: W/System.err(2263): at org.json.JSONTokener.nextValue(JSONTokener.java:103)
10-02 18:49:09.985: W/System.err(2263): at org.json.JSONTokener.readObject(JSONTokener.java:385)
10-02 18:49:09.985: W/System.err(2263): at org.json.JSONTokener.nextValue(JSONTokener.java:100)
10-02 18:49:09.985: W/System.err(2263): at org.json.JSONObject.<init>(JSONObject.java:154)
10-02 18:49:09.985: W/System.err(2263): at org.json.JSONObject.<init>(JSONObject.java:171)
10-02 18:49:09.985: W/System.err(2263): at com.tarasoft.shop.HTTPRequests$SendRequest.doInBackground(HTTPRequests.java:36)
10-02 18:49:09.985: W/System.err(2263): at com.tarasoft.shop.HTTPRequests$SendRequest.doInBackground(HTTPRequests.java:1)
10-02 18:49:09.985: W/System.err(2263): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-02 18:49:09.985: W/System.err(2263): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-02 18:49:09.985: W/System.err(2263): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-02 18:49:09.985: W/System.err(2263): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-02 18:49:09.985: W/System.err(2263): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-02 18:49:09.985: W/System.err(2263): at java.lang.Thread.run(Thread.java:856)
Update 2 You should know that these keys "1", "2" etc. can be strings too so I don't use them to sort the json, I use the only to see if the order is correct. Instead of these "1", "2" etc. there will be strings.
Upvotes: 0
Views: 307
Reputation: 4593
JSONArray is ordered and can contain collections as values, in other words other jsonArrays. Make the questions part a jsonArray and then put it inside the jsonObject like this:
JSONArray questionsArray = new JSONArray();
JSONArray one = new JSONArray();
one.put("ELSYS")
one.put("ELSYS2")
one.put("ELSYS3")
one.put("ELSYS4")
JSONObject firstObject = new JSONObject()
firstObject.put("question", "6*2")//(key, value)
firstObject.put("answers", one)
questionsArray.put(firstObject )
JSONArray two = new JSONArray();
two.put("1024")
two.put("2048")
two.put("512")
two.put("64")
JSONObject secondObject = new JSONObject()
secondObject.put("question", "hello world?")
secondObject.put("answers", two)
questionsArray.put(secondObject)
//do this for all your items then add the questions array to your jsonObject
JSONObject mainJsonObj = new JSONObject()
mainJsonObj.put("questions", questionsArray);
//then when you need the data you will go like this:
questionsArray = mainJsonObj.optJSONArray("questions")
//then you have your ordered array containing your question/answer objects, the answers of which will be ordered. To get them you will have to get by index like:
objOne = questionsArray.get(0) //etc
String question = objOne.optString("question")
JSONArray answers = objOne.optString("answers")
Something along those lines, you could try making it a bit more elegant, this is only to demonstrate the basic idea. Alternatively you will have to look at a different data structure like linkedHashMap/arrayList
EDIT
Seeing that you need the keys as you require the string values you will have to use a combination of json objects and array. The important thing to remember is this JSONArray will maintain its order and JSONObject will not maintain its order (and there isnt much you can do about it). I will edit the above code for ease of readability. The way initially envisaged it in your question wont be possible to maintain its order, so this solution might not be exactly like you want it, but its probably the closest your going to get using json unfortunately. You might want to wrap the sections in a function and pass the question and answer list in as values, that way you dont need to repeat all code so much, but dont worry about that for now. First see if this solution will work for your needs.
Upvotes: 1
Reputation: 11749
From the JSON specification at http://www.json.org/
An object is an unordered set of name/value pairs
Don't worry about the order, there's not much you can do about it.
Upvotes: 2