【发布时间】:2016-04-09 03:52:28
【问题描述】:
我搜索过类似的问题,但没有成功。
我有类似的 JSON 数据 http://oep.esy.es/item_connect.php
{
"items":[
{
"id":"7",
"name":"bir",
"image":"6051-1.png",
"number":"1",
"shape":"\u015eekilsiz",
"color":"turuncu"
},
{
"id":"8",
"name":"d\u00f6rt",
"image":"1727-4.png",
"number":"1",
"shape":"\u015eekilsiz",
"color":"turuncu"
},
{
"id":"9",
"name":"iki",
"image":"2667-2.png",
"number":"1",
"shape":"\u015eekilsiz",
"color":"turuncu"
},
{
"id":"10",
"name":"\u00fc\u00e7",
"image":"9998-3.png",
"number":"1",
"shape":"\u015eekilsiz",
"color":"turuncu"
},
{
"id":"11",
"name":"be\u015f",
"image":"7005-5.png",
"number":"1",
"shape":"\u015eekilsiz",
"color":"turuncu"
},
{
"id":"12",
"name":"alt\u0131",
"image":"9377-6.png",
"number":"1",
"shape":"\u015eekilsiz",
"color":"turuncu"
},
{
"id":"13",
"name":"elma",
"image":"1656-1001.png",
"number":"1",
"shape":"\u015eekilsiz",
"color":"k\u0131rm\u0131z\u0131"
}
],
"success":1
}
先是项目词,最后是成功词。有出阵。当我删除它们时,我可以成功。
StringBuilder sb = new StringBuilder();
sb.append(json+"\n");//Log.d(TAG, "sb "+sb.toString());
}
try {Log.d(TAG, "xx ");
JSONArray mJsonArray = new JSONArray(sb);Log.d(TAG, "xx x");
JSONObject mJsonObject = new JSONObject();
这里不能去 xx x 日志,它进入错误捕获。
但是当我删除 json 格式的页眉和页脚时,它可以工作。我应该使用字符串操作来删除那些还是正则表达式?
编辑:
我是这样解决的:
try {Log.d(TAG, "xx ");
JSONArray(sb);Log.d(TAG, "xx x");
JSONObject mJsonObject = new JSONObject();//i use this to get invidial value of json, do i need?
JSONObject jsono = new JSONObject(sb.toString());
Log.d(TAG,"jsono "+jsono.toString());
JSONArray items = jsono.getJSONArray("items");
Log.d(TAG,"items "+items);
Log.d(TAG,"items length "+items.length());
for (int i = 0; i < items.length(); i++) {
// Log.d(TAG, "xx " +mJsonObject.getString("image"));
Log.d(TAG, "string: "+items.getJSONObject(i).getString("image"));//i need image urls
}
这段代码可以正常工作,但有时会出现 try catch 错误,我看不到任何错误。有时它需要很长时间,大约 1-2 分钟,而且最终几乎不会失败。为什么会这样?
【问题讨论】: