1

My server returned the following string using the jsonencode() in the php code after sending a POST passing variables for the query.

{"distance":"0.00194210443015968","usrlat":"38.5817","usrlong":"-77.3245","globalid":"245"}{"distance":"4.94445650874035","usrlat":"38.6501","usrlong":"-77.2975","globalid":"233"}{"distance":"4.94445650874035","usrlat":"38.6501","usrlong":"-77.2975","globalid":"242"}

Code:

        try 
        { etc.. connection details..

            request = new OutputStreamWriter(connection.getOutputStream()); 
            request.write(parameters); 
            request.flush(); 
            request.close();             
            String line = "";                
            //Convert response to a string
            InputStreamReader isr = new InputStreamReader(connection.getInputStream()); 
            BufferedReader reader = new BufferedReader(isr); 
            StringBuilder sb = new StringBuilder(); 
            while ((line = reader.readLine()) != null) 
            { 

                sb.append(line + "\n");

            } 
            // Response from server will be stored in response variable.                 
            response = sb.toString(); 

            //try parse the string to a JSON object
            try{

                jObject = new JSONObject(response);                 

            }catch(JSONException e){...

Background - This simple bit of code has produced a jObject which holds only the first element (object) from response. I have tried changing the reponse to an jArray by inserting square brackets before and after, however the elements (objects) from the response are not seperated by a comma. Considered interating through the response to insert comma's however the same root problem exists... parsing and interation. Additionally, I have created a class with properties according to the response. No luck there because the same root problem exist...Parsing and iteration. I have scoured the net, only to discover that JSON is an extremely easy and lite weight alt to XML. I have visted my local book store to discover that JSON is not a book worthy topic...yet. Finally, I have turned to GSON for some clarity.

Question - Using JSON or GSON how do I deserialize and iterate through the response to create useable objects in my android application? Am I asking the right question in my search for a solution?

C_Ash2012
  • 13
  • 3

1 Answers1

1

You basically hit on your issue - that the text is not a valid JSON array. So you have two options:

  1. Preprocess the JSON to make it a valid JSON array
  2. Read each line one by one and create a JSONObject for each line, then manually add each object to a JSONSArray or a plain old java array or collection

FYI - This assumes you have no control over the server side. If you do, change that code to make it a valid JSON array

Mike Marshall
  • 7,788
  • 4
  • 39
  • 63
  • mjmarsh thanks for the reponse. I am a newbie no doubt. Prepocessing brings me to iteration. the response is all concated. Would love to bypass the JSONArray still need iteration through string right?. Finally, the server is on a hosted platform, have reviewed the ini. Is the jsonencode() adjustable on mySQL? – C_Ash2012 Jan 17 '12 at 01:04
  • I have changed the server output to [{"distance":"0.00194210443015968","usrlat":"38.5817","usrlong":"-77.3245","globalid":"245"}][{"distance":"4.94445650874035","usrlat":"38.6501","usrlong":"-77.2975","globalid":"233"}][{"distance":"4.94445650874035","usrlat":"38.6501","usrlong":"-77.2975","globalid":"242"}] Additionally, I have included jArray = new JSONArray(response); I have tried an iterative for loop but only pulls in the first array. – C_Ash2012 Jan 17 '12 at 02:56
  • mjmarsh - THANK YOU. I modified the output of the server. I used the following link to add each proper JSONObject returned to a proper formatted JSON Array. It was manageable to do it at the server than modify in Java. I used http://stackoverflow.com/questions/383631/json-encode-mysql-results to create the proper array. I used http://jsonformatter.curiousconcept.com/ to validate the response. Now I will use either JSON or GSON in the app. Thanks Again! – C_Ash2012 Jan 17 '12 at 14:54