org.json.JSONException:在字符550处预期字面值

8

我正在尝试从资产文件夹读取JSON文件。但是我遇到了以下异常:

org.json.JSONException: 在第550个字符处预期字面值

我搜索了很多东西,但没有找到相关的内容。这是我的JSON文件:

{
"resultCount": 3,
"SearchedTerm": "Wada Pav",
"results": [
{
  "locationname": "Mahableshwar Hotel",
  "locationid": "12345",
  "locationaddress" : "baner, Pune",
  "dishrating": "4",
  "dishname": "Wada Pav",
  "dishid": "234",
  "dishcategory": "Snacks",
  "dishnotes": "Spicy Wada Pav",
  "dishpreviewurl":"http://xxx.yyy.zzz/mahableshwar/1.jpg",
  "dishtotalvotes": "9999",
  "friendslistvoted": {
            "friendscount": "3",
            "names": ["Santosh","Sandip","Arvind"],
          }
  "dishimageurl": "http://xxx.yyy.zzz/mahableshwar/2.jpg",
  "mylastrating": "4"
 } 
]
}

我发现550上的JSON对象是"names": ["Santosh","Sandip","Arvind"],。我正在尝试解决它,但不知道我的代码出了什么问题。这是我的代码:
try {

        input = assetManager.open("dishum-sample-search-results-json.txt");
        int size = input.available();
        byte[] buffer = new byte[size];
        input.read(buffer);
        input.close();
        
        // byte buffer into a string
        String text = new String(buffer);
        try{
        JSONObject jsonObject = new JSONObject(text);
         
        JSONObject searchedTerm = jsonObject.getJSONObject(TAG_SEARCHEDTERM);           
        JSONArray results = searchedTerm.getJSONArray(TAG_RESULTS);         
        for(int i=0; i<results.length(); i++)
        {
            JSONObject dishResult = results.getJSONObject(i);
            String locationName = dishResult.getString(TAG_lOCATIONNAME);
            int locationId = dishResult.getInt(TAG_LOCATIONID);
            String locationAddress = dishResult.getString(TAG_LOCATIONADDRESS);
            String dishNotes = dishResult.getString(TAG_DISHNOTES);
            int dishRating = dishResult.getInt(TAG_DISHRATING);
            JSONObject friendListVoted = dishResult.getJSONObject(TAG_FRIENDLISTVOTED);
            int friendCount = friendListVoted.getInt(TAG_FRIENDCOUNT);
            map.put(TAG_lOCATIONNAME, locationName);
            map.put(TAG_LOCATIONID, String.valueOf(locationId));
            map.put(TAG_LOCATIONADDRESS, locationAddress);
            map.put(TAG_DISHNOTES, dishNotes);
            map.put(TAG_DISHRATING, String.valueOf(dishRating));
            map.put(TAG_FRIENDCOUNT, String.valueOf(friendCount));              
            // adding HashList to ArrayList
            songsList.add(map); 
        }
        }
        catch(JSONException e)
        {
            Toast.makeText(this, "Error in parsing json file"+e, Toast.LENGTH_LONG).show();
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我也调试了我的代码,但当控制流程到达 JSONObject jsonObject = new JSONObject(text); 这一行时,它会抛出异常并进入第一个 catch 块。我该如何解决这个问题?

(编辑注:此问题中删除了两个代码项,我无法弄清楚原因。由于接受的答案至少依赖其中一个,我已将它们都恢复。) - halfer
4个回答

18

您的 JSON 格式不正确。
您的 JSON 应该像这样

{
    "resultCount": 3,
    "SearchedTerm": "Wada Pav",
    "results": [
        {
            "locationname": "Mahableshwar Hotel",
            "locationid": "12345",
            "locationaddress": "baner, Pune",
            "dishrating": "4",
            "dishname": "Wada Pav",
            "dishid": "234",
            "dishcategory": "Snacks",
            "dishnotes": "Spicy Wada Pav",
            "dishpreviewurl": "http://xxx.yyy.zzz/mahableshwar/1.jpg",
            "dishtotalvotes": "9999",
            "friendslistvoted": {
                "friendscount": "3",
                "names": [
                    "Santosh",
                    "Sandip",
                    "Arvind"
                ]
            },
            "dishimageurl": "http://xxx.yyy.zzz/mahableshwar/2.jpg",
            "mylastrating": "4"
        }
    ]
}

在使用JSON之前,建议先使用JSON验证器(例如JSLint)进行验证。


9
我使用以下方法获取标准的JSON格式。 这个更好。
    public static String convertStandardJSONString(String data_json) {
        data_json = data_json.replaceAll("\\\\r\\\\n", "");
        data_json = data_json.replace("\"{", "{");
        data_json = data_json.replace("}\",", "},");
        data_json = data_json.replace("}\"", "}");
        return data_json;
    }

太好了,它起作用了,但我不知道为什么我一开始需要这个,因为我正在使用drupal_json_encode!!! Drupal不是标准的吗? - Milad.Nozari

4

1
这对我有效。
    public static String convertJSONString(String data) {
    data = data.replace("\\", "");
    return data; }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接