skip to Main Content

I’m trying to get the LAT/LNG values from my json(as Double) seen here:

{
    "placeNames": [
        {
            "name": "Test",
            "lat": "0",
            "lon": "0"
        },
        {
            "name": "Üsküdar Bağlarbaşı Yolu",
            "lat": "1",
            "lon": "2"
        },
        {
            "name": "Çamlıca",
            "lat": "3",
            "lon": "4"
        }
    ]
}

And put markers on location in google maps seen here:

JSONParser jsonParser = new JSONParser();

try {
    JSONObject jsonObject = (JSONObject) jsonParser.parse(GetJson(getContext())); //TODO: READ JSON FILE
    JSONArray lang = (JSONArray) jsonObject.get("placeNames");
    for (int i = 0; i < lang.size(); i++) {
        JSONObject explrObject = (JSONObject) lang.get(i);
        String PlaceName = (String) explrObject.get("name");
        MarkerOptions marker3 = new MarkerOptions();
        Double LAT = ((Number) explrObject.get("lat")).doubleValue();
        Double LNG = ((Number) explrObject.get("lon")).doubleValue();
        marker3.position(new LatLng(LAT, LNG)).title(PlaceName);
    }
}

The problem im getting is the fact that LNG/LAT values seem to be coming in as string even though theyre numbers on my JSON file,Any help is appreciated.:)

2

Answers


  1. Maybe you can try:

    Double LAT = Double.parseDouble(explrObject.getString("lat"));
    

    or

    Double LAT = Double.parseDouble((String) explrObject.get("lat"));
    
    Login or Signup to reply.
  2. Following is a valid JSON syntax with double/ float values that you can use to construct your json as,

    {
        "placeNames":[
            {
                "name":"Test",
                "lat":29.470237,
                "lon":107.722125
            }
        ]
    }
    

    also, you can refer to this website for validating your JSON syntax online in case of any doubts. And also, your above JSON parser logic will work on this JSON data.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search