我正在尝试从 asset 文件夹中读取 JSON 文件。但我得到以下异常:
org.json.JSONException: Expected literal value at character 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 块。我怎么解决这个问题?