Android Studio:出现解析错误,无法找到问题

时间:2017-06-13 19:22:43

标签: android json listview parsing

我有一个JSONArray,其格式如下:

[
{
"type": "fuel",
"name": "Esso",
"address": "Frankfurter Straße 65",
"lat": 49.8848387,
"lon": 8.6520691 },
{
"type": "amenity",
"name": "Hauptbahnhof",
"address": "Am Hauptbahnhof 20",
"lat": 49.8725, 
"lon": 8.628889,
"icon": "bahnhof.jpg" }
]

它继续。

当我打开我希望我的JSONArray在Listview中的活动的那一刻,我得到了一个Toast with the catch of my catchblock"错误解析:.."它向我展示了吐司中的整个JSONFile。

有人可以解释为什么会这样,我无法找到我的错误。我会向你们展示我的Tryblock,其中会出现错误。

protected Void doInBackground(Void... arg0) {
        HttpHandler sh = new HttpHandler();
        // Making a request to url and getting response
        String url = "...excluded link...";
        String jsonStr = sh.makeServiceCall(url);

        Log.e(TAG, "Response from url: " + jsonStr);
        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                JSONArray contacts = jsonObj.getJSONArray("contacts");

                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);
                    String type = c.getString("type");
                    String name = c.getString("name");
                    String address = c.getString("address");
                    String lat = c.getString("lat");
                    String lon = c.getString("lon");
                    String icon = c.getString("icon");

                    // tmp hash map for single contact
                    HashMap<String, String> contact = new HashMap<>();

                    // adding each child node to HashMap key => value
                    contact.put("type", type);
                    contact.put("name", name);
                    contact.put("address", address );
                    contact.put("lat", lat);
                    contact.put("lon", lon);
                    contact.put("icon", icon);

                    // adding contact to contact list
                    contactList.add(contact);
                }
            } catch (final JSONException e) {
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }
                });

            }

        } else {
            Log.e(TAG, "Couldn't get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get json from server. Check LogCat for possible errors!",
                            Toast.LENGTH_LONG).show();
                }
            });
        }

        return null;
    }

1 个答案:

答案 0 :(得分:1)

假设你的JSON是正确的,它看起来像:

 {
  "contacts": [
    {
      "type": "fuel",
      "name": "Esso",
      "address": "Frankfurter Straße 65",
      "lat": 49.8848387,
      "lon": 8.6520691
    },
    {
      "type": "amenity",
      "name": "Hauptbahnhof",
      "address": "Am Hauptbahnhof 20",
      "lat": 49.8725,
      "lon": 8.628889,
      "icon": "bahnhof.jpg"
    }
  ]
}

现在如何使用CHECK for KEY解析代码:

JSONObject jsonObj = new JSONObject(jsonStr);

// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("contacts");

// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
    JSONObject c = contacts.getJSONObject(i);
    String type = c.getString("type");
    String name = c.getString("name");
    String address = c.getString("address");
    String lat = c.getString("lat");
    String lon = c.getString("lon");
    String icon;
    if(c.has("icon")){
        //your json is having "icon" Key, get the value
        icon = c.getString("icon");
    }
    else{
        //your json is NOT having "icon" Key,  assign a dummy value 
        icon = "/default/icon_url(if_any)";
    }
}

这样做,如果您要存储的任何或任何其他字符串不存在,或者您可以将其保留为空,它将指定默认的图标URL,如果图标存在,您将获得该网址。 你可以要求对这个答案有任何疑问。

快乐编码.. :)