0
[

    {
        "description": "My home", 
        "name": "Sweet Home", 
        "point": {
            "lat": 22.890976, 
            "long": 90.459097
        }, 
        "type": 1,
        "cid": "5319197376176516414"
    }

This is my json file for parsing information. Here is my code for parsing name and lng.

BufferedReader jsonReader = new BufferedReader(new InputStreamReader(this.getResources().openRawResource(R.raw.map)));
        StringBuilder jsonBuilder = new StringBuilder();
        try {
            for (String line = null; (line = jsonReader.readLine()) != null;) {
                jsonBuilder.append(line).append("\n");
            }

            JSONTokener tokener = new JSONTokener(jsonBuilder.toString());
            JSONArray jsonArray = new JSONArray(tokener);

                JSONObject jsonObject = jsonArray.getJSONObject(0);

                String title = jsonObject.getString("name");
                String lhg = jsonObject.getJSONObject("point").getString("lng");
} catch (FileNotFoundException e) {
            Log.e("jsonFile", "file not found");
        } catch (IOException e) {
            Log.e("jsonFile", "ioerror");
        } catch (JSONException e) {
            Log.e("jsonFile", "error while parsing json");
        }
    }

}

It show's me an exception error while parsing json. How do i solve that? What was my problem?

4
  • 2
    Your JSON doesn't look valid . Commented Jun 17, 2013 at 11:38
  • 2
    @TheNewIdiot is right, there is a missing closing bracket. Or is this a copy/paste error? Commented Jun 17, 2013 at 11:39
  • 1
    Please log the exception Log.e("jsonFile",e.getMessage()); rather than custom exception message. Commented Jun 17, 2013 at 11:41
  • @TheNewIdiot I think that's just a copy/paste error Commented Jun 17, 2013 at 11:42

2 Answers 2

5

Because "Point" in your JSON object never contains a property called "lng"

 String lhg = jsonObject.getJSONObject("point").getString("lng")

it does contain one named "long"

"point": {
            "lat": 22.890976, 
            "long": 90.459097
        }, 

So the code to fetch the longitude should look like this:

 String lhg = jsonObject.getJSONObject("point").getString("long")
Sign up to request clarification or add additional context in comments.

1 Comment

Additionally, latitude and longitude aren't expressed as strings for a reason (they're actually a vector in a numeric coordinate system)
1
 String lhg = jsonObject.getJSONObject("point").getString("lng")  use "long" instead of lng.

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.