简体   繁体   中英

Android - JSON / GSON Parsing

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM