简体   繁体   English

Android Studio - JSON 数据

[英]Android Studio - JSON Data

I've made a "Google maps Activity" on Android Studio, but to add markers i'm using JSON.我在 Android Studio 上做了一个“谷歌地图活动”,但是为了添加标记,我使用了 JSON。 (my first ever time using JSON) (我第一次使用 JSON)

Basically when the google maps opens, it loads a 'marker' on the location (String strJson) which contains the data.基本上,当谷歌地图打开时,它会在包含数据的位置(字符串 strJson)上加载一个“标记”。 The marker displays the name and country, but I can't seem to get the 'latlng' to work.标记显示名称和国家/地区,但我似乎无法让 'latlng' 起作用。 I've tried...我试过了...

Int latlng = Integer.parseInt(jsonObject.optString("latlng").toString()); 

But the 'gMap.addMarker' doesn't allow 'Int'但是“gMap.addMarker”不允许“Int”

Does anyone know how i could do this?有谁知道我怎么能做到这一点?

CODE:代码:

String strJson= "{\"Location\" :[{\"name\":\"Gladiator Fitness\",\"latlng\":[\"53.2286362\",\"-0.5856869\"],\"country\":\"GB\"}]}";

try {
    JSONObject jsonRootObject = new JSONObject(strJson);
    //Get the instance of JSONArray that contains JSONObjects
    JSONArray jsonArray = jsonRootObject.optJSONArray("Location");

    //Iterate the jsonArray and print the info of JSONObjects
    for(int i = 0; i < jsonArray.length(); i++){
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        //Search the database for the "data"
        String name = jsonObject.optString("name").toString();
        String country = jsonObject.optString("country").toString();
        //Add marker for each location
        gMap.addMarker(new MarkerOptions().position(new LatLng(53.2286362,-0.5856896)).title(name).snippet(country));
    }

Here is a screenshot of the same code.是相同代码的屏幕截图。 Thanks in advance.提前致谢。

    String strJson= "{\"Location\" :[{\"name\":\"Gladiator Fitness\",\"latlng\":[\"53.2286362\",\"-0.5856869\"],\"country\":\"GB\"}]}\n";

    try {
        JSONObject jsonRootObject = new JSONObject(strJson);
        JSONArray jsonArray = jsonRootObject.optJSONArray("Location");

        //Iterate the jsonArray and print the info of JSONObjects
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            //Search the database for the "data"
            String name = jsonObject.optString("name").toString();
            String country = jsonObject.optString("country").toString();
            JSONArray latlng = jsonObject.optJSONArray("latlng");
            double lat = latlng.optDouble(0);
            double lng = latlng.optDouble(1);

            Log.d("JSON", "coords: " + lat + " " + lng);
        }
    } catch (Exception e) {
        Log.d("JSON", "e: " + e);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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