1 Replies - 10764 Views - Last Post: 29 December 2011 - 08:51 AM

#1 pangjd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-September 08

JSONArray to JSONObject Question

Posted 29 December 2011 - 02:21 AM

public void JSONData(String string){
    	try{
    	String string1 = string.substring(1, string.length());
            JSONArray jArray = new JSONArray(string1);
            
            SQLiteDB db = new SQLiteDB(this);
            db.open();
            for(int i=0;i<jArray.length();i++){
                    JSONObject json_data = jArray.getJSONObject(i);
                    System.out.println(json_data.toString());
                    JSONArray print2 = json_data.names();
                    String print = print2.get(i).toString();
                   // System.out.println(print);
                    Log.i("log_tag",print+" :"+json_data.getString("username")+
                            "password :"+json_data.getString("password")+
                            "FEB INR :"+json_data.getString("FEB 11")
                            
                    );
                    db.createEntry(json_data.getString("username"), json_data.getString("FEB 11"));
                    //Get an output to the screen
                    string1 += "\n\t" + jArray.getJSONObject(i); 
                    
                    //System.out.println(string1);
            }
            db.close();
    }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
    }
    
    }



my json string is --> Y[{"username":"test","password":"test","FEB 11":"2.4","MAR 11":"2.5","APR 11":"3","MAY 11":"2.8"}]

when i print out System.out.println(json_data.toString()); it gives me
{"MAY 11":"2.8","FEB 11":"2.4","username":"test",,"MAR 11":"2.5","password":"test","APR 11":"3"} instead of the correct order of {"username":"test","password":"test","FEB 11":"2.4","MAR 11":"2.5","APR 11":"3","MAY 11":"2.8"}

how can i make it into the correct ordering?

Is This A Good Question/Topic? 0
  • +

Replies To: JSONArray to JSONObject Question

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: JSONArray to JSONObject Question

Posted 29 December 2011 - 08:51 AM

The array contains 1 element. Each name/value should be treated as an array element.

String json = "Y[{\"username\":\"test\"},{\"password\":\"test\"},{\"FEB 11\":\"2.4\"},{\"MAR 11\":\"2.5\"},{\"APR 11\":\"3\"},{\"MAY 11\":\"2.8\"}]";
JSONArray jsonArr = new JSONArray(json.substring(1));
System.out.println(jsonArr);


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1