2

I am reading multiple JSONObject from a file and converting into a string using StringBuilder.

These are the JSON Objects.

{"Lng":"-1.5908601","Lat":"53.7987816"}
{"Lng":"-2.5608601","Lat":"54.7987816"}
{"Lng":"-3.5608601","Lat":"55.7987816"}
{"Lng":"-4.5608601","Lat":"56.7987816"}
{"Lng":"-5.560837","Lat":"57.7987816"}
{"Lng":"-6.5608294","Lat":"58.7987772"}
{"Lng":"-7.5608506","Lat":"59.7987823"}

How to convert into a string?

Actual code is:-

BufferedReader reader = new BufferedReader(new InputStreamReader(contents.getInputStream()));
        StringBuilder builder = new StringBuilder();
        String line;

        try {
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        }
        catch(IOException e)
        {
            msg.Log(e.toString());
        }

        String contentsAsString = builder.toString();
        //msg.Log(contentsAsString);

I tried this code

JSONObject json = new JSONObject(contentsAsString);

Iterator<String> iter = json.keys();
  while(iter.hasNext())
    {
       String key = iter.next();
         try{
             Object value = json.get(key);
             msg.Log("Value :- "+ value);
         }catch(JSONException e)
          {
              //error
          }
    }

It just gives first object. How to loop them?

11
  • 1
    What is contentsAsString? Commented Apr 4, 2015 at 22:16
  • but you already have it in a string format in your stringbuilder, what exactly are you trying to do. Commented Apr 4, 2015 at 22:17
  • Joel - contentAsString is a list of objects as shown above, which is in a string format. Commented Apr 4, 2015 at 22:19
  • faljbour - I want them to be in a separate string variable to use them for example. for(int i=0; i<=json.length(); i++) { String lat = json.getString("Lat"); String lng = json.getString("Lng"); Log.i("TAG", lat + lng) } i want to get those separate values into a string to use them. Commented Apr 4, 2015 at 22:23
  • @faljbour - sry i cant make them in a proper format in comment. Commented Apr 4, 2015 at 22:24

2 Answers 2

2

try this and see how it works for you,

BufferedReader in
   = new BufferedReader(new FileReader("foo.in"));

ArrayList<JSONObject> contentsAsJsonObjects = new ArrayList<JSONObject>();
while(true)
{
  String str = in.readLine();
  if(str==null)break;
  contentsAsJsonObjects.add(new JSONObject(str));
}

for(int i=0; i<contentsAsJsonObjects.size(); i++) 
{ 
  JSONObject json = contentsAsJsonObjects.get(i);
  String lat = json.getString("Lat"); 
  String lng = json.getString("Lng"); 
  Log.i("TAG", lat + lng) 
}
Sign up to request clarification or add additional context in comments.

Comments

2

What you do is you are loading multiple JSON objects into one JSON object. This does not make sense -- it is logical that only the first object is parsed, the parser does not expect anything after the first }. Since you want to loop over the loaded objects, you should load those into a JSON array.

If you can edit the input file, convert it to the array by adding braces and commas

[
    {},
    {}
]

If you cannot, append the braces to the beginning of the StringBuilder and append comma to each loaded line. Consider additional condition to eliminate exceptions caused by inpropper input file.

Finally you can create JSON array from string and loop over it with this code

JSONArray array = new JSONArray(contentsAsString);

for (int i = 0; i < array.length(); ++i) {
    JSONObject object = array.getJSONObject(i);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.