当前位置: 代码迷 >> Android >> 请教关于json解析的有关问题
  详细解决方案

请教关于json解析的有关问题

热度:73   发布时间:2016-04-28 02:15:19.0
请问关于json解析的问题
请问,关于json解析的时候 出现了以下这种错误提示:
 org.json.JSONException: Index 1 out of range [0..1)
麻烦会的朋友帮忙回复下,谢谢啦

代码如下:(string 为需要解析的json)
private static List<Chat>  parsejsonchatget(String string) throws Exception {
List<Chat> list = new ArrayList<Chat>();
JSONObject obj1 = new JSONObject(string);
int total = obj1.getInt("total");
JSONArray jsonArray = obj1.getJSONArray("data"); 
for( int i=0;i<jsonArray.length();i++ ) {
JSONObject item = jsonArray.getJSONObject(i);
String chatid = item.getString("chatid");
String userid = item.getString("userid");
String photo = item.getString("photo");
String nickname = item.getString("nickname");

String fullname = item.getString("fullname");
String sex = item.getString("sex");
String from1 = item.getString("from1");
String from2 = item.getString("from2");

String like = item.getString("like");
String type = item.getString("type");
String content = item.getString("content");

String time = item.getString("time");


JSONObject replyobject= new JSONObject(item.getString("reply"));
String replytotal = replyobject.getString("total");  //以上的代码是正确的

JSONArray jsonArray1 = replyobject.getJSONArray("data");  // 这个是获取 reply里边的 data数组,这样写对吗?


for( int j=0;j<jsonArray1.length();j++ ) {
JSONObject replyitem = jsonArray1.getJSONObject(i);
String replychatid = replyitem.getString("chatid");
Log.d(TAG,"yong parsejsonuserhistory replychatid "+replychatid);
}
。。。
                。。。
//list.add(new ChatGet(total,listid,listtype,listtitle,listtime));
}
return list;
}

我用 格式工具 转换后,需要解析的json数据如下:
android:theme="@android:style/Theme.NoTitleBar"

{
    "total": 8,
    "data": [
        {
            "chatid": "4d9738010ce6ce3aa50d1131ae3daea6",
            "userid": "8012be515cc9244ef263e91d2b442349",
            "photo": "",
            "nickname": "Tyh",
            "fullname": "",
            "sex": "1",
            "from1": "",
            "from2": "",
            "like": "1",
            "type": "txt",
            "content": "大动干戈",
            "time": "2015-03-05 09:01:14",
            "reply": {
                "total": 2,
                "data": [
                    {
                        "chatid": "701434a742f39df3a85f488ac150f47d",
                        "userid": "8012be515cc9244ef263e91d2b442349",
                        "photo": "",
                        "nickname": "Tyh",
                        "fullname": "",
                        "sex": "1",
                        "from1": "",
                        "from2": "",
                        "content": "dfhh",
                        "time": "2015-03-05 09:02:47"
                    },
                    {
                        "chatid": "a27cd18b86763aa0d9aa7f5e5b34ca2f",
                        "userid": "8012be515cc9244ef263e91d2b442349",
                        "photo": "",
                        "nickname": "Tyh",
                        "fullname": "",
                        "sex": "1",
                        "from1": "",
                        "from2": "",
                        "content": "dfg",
                        "time": "2015-03-05 09:02:38"
                    }
                ]
            }
        },
        {
            "chatid": "c2cbac722a1ca724963931b215a76e70",
            "userid": "5cc464fc0d4b0c400b07729abd25c726",
            "photo": "http:\/\/eyao.digigd.com\/img.php\/95f7912977b37bf8edf7b44704ce306b\/w_80\/h_80",
            "nickname": "辉",
            "fullname": "范万辉",
            "sex": "1",
            "from1": "销售企业",
            "from2": "医药公司采购人员",
            "like": "0",
            "type": "pic",
            "content": "...",
            "pic": [
                "http:\/\/eyao.digigd.com\/img.php\/6d75b9b8391f12ded86a1a448db44e6a"
            ],
            "time": "2015-03-04 17:20:33",
            "reply": {
                "total": 1,
                "data": [
                    {
                        "chatid": "c37a15c510a4039e9f7507296d5bc085",
                        "userid": "5cc464fc0d4b0c400b07729abd25c726",
                        "photo": "http:\/\/eyao.digigd.com\/img.php\/95f7912977b37bf8edf7b44704ce306b\/w_80\/h_80",
                        "nickname": "辉",
                        "fullname": "范万辉",
                        "sex": "1",
                        "from1": "销售企业",
                        "from2": "医药公司采购人员",
                        "content": "这样",
                        "time": "2015-03-05 10:26:57"
                    }
                ]
            }
        },
        {
            "chatid": "d9c44f518c7a61430171ddf0fc74959e",
            "userid": "5cc464fc0d4b0c400b07729abd25c726",
            "photo": "http:\/\/eyao.digigd.com\/img.php\/95f7912977b37bf8edf7b44704ce306b\/w_80\/h_80",
            "nickname": "辉",
            "fullname": "范万辉",
            "sex": "1",
            "from1": "销售企业",
            "from2": "医药公司采购人员",
            "like": "0",
            "type": "pic",
            "content": "你的时候你",
            "pic": [
                "http:\/\/eyao.digigd.com\/img.php\/1a8b54581c287a553ef24a137b99b587",
                "http:\/\/eyao.digigd.com\/img.php\/2dac5499f3286e6428f75daa2490dcc9"
            ],
            "time": "2015-03-04 16:36:22",
            "reply": {
                "total": 2,
                "data": [
                    {
                        "chatid": "ebc97f6e3ae21a59a27129031c65f0f9",
                        "userid": "cbe6c129803db916473e3d2ede3f2842",
                        "photo": "http:\/\/eyao.digigd.com\/img.php\/2dde3440abc47ad91bcd399c69b3e65b\/w_80\/h_80",
                        "nickname": "阿辉昵称最多8字",
                        "fullname": "范",
                        "sex": "1",
                        "from1": "生产企业",
                        "from2": "研发人员",
                        "content": "你是不过",
                        "time": "2015-03-04 16:45:02"
                    },
                    {
                        "chatid": "e3904178ef7fe6766c8a01e339a7826a",
                        "userid": "cbe6c129803db916473e3d2ede3f2842",
                        "photo": "http:\/\/eyao.digigd.com\/img.php\/2dde3440abc47ad91bcd399c69b3e65b\/w_80\/h_80",
                        "nickname": "阿辉昵称最多8字",
                        "fullname": "范",
                        "sex": "1",
                        "from1": "生产企业",
                        "from2": "研发人员",
                        "content": "你的心中只有",
                        "time": "2015-03-04 16:44:15"
                    }
                ]
            }
        },
        {
            "chatid": "09a2c16ed6db66942b70aba1dd0df557",
            "userid": "f14d54bef478c7e802e7832aea28eb5b",
            "photo": "",
            "nickname": "sk2",
            "fullname": "sk",
            "sex": "2",
            "from1": "医疗机构",
            "from2": "护士",
            "like": "0",
            "type": "txt",
            "content": "一套房子\n人傻钱多\n人人上跟我\n热腾腾刚刚突然\n和你\n佛山\n饭堂\n茹毛饮血和尚\n放心啦\n跟上次\n放床上\n听不懂\n挺配\n放心\n茹茹\n给我\n",
            "time": "2015-03-04 15:55:07",
            "reply": {
                "total": 1,
                "data": [
                    {
                        "chatid": "1fc2b8049a804b3cefc1e5709376886f",
                        "userid": "20931c16fc5c1a83b71a928e68862556",
                        "photo": "http:\/\/eyao.digigd.com\/img.php\/33f9f32943f90b4cfa35e513cca726e4\/w_80\/h_80",
                        "nickname": "小杨",
                        "fullname": "",
                        "sex": "2",
                        "from1": "医疗机构",
                        "from2": "医生",
                        "content": "111",
                        "time": "2015-03-04 16:53:56"
                    }
                ]
            }
        },
        {
            "chatid": "a7a24646a72264e666850aaa08b155af",
            "userid": "cbe6c129803db916473e3d2ede3f2842",
            "photo": "http:\/\/eyao.digigd.com\/img.php\/2dde3440abc47ad91bcd399c69b3e65b\/w_80\/h_80",
            "nickname": "阿辉昵称最多8字",
            "fullname": "范",
            "sex": "1",
            "from1": "生产企业",
            "from2": "研发人员",
            "like": "3",
            "type": "txt",
            "content": "你们都江堰峨眉山景区门票",
            "time": "2015-03-03 17:52:32",
            "reply": {
                "total": 5,
                "data": [
                    {
                        "chatid": "52a7d5d943e4568b9b3c4eca5f2b826c",
                        "userid": "81800078a64288216f21b3a9285523d2",
                        "photo": "http:\/\/e


------解决思路----------------------
for( int j=0;j<jsonArray1.length();j++ ) {
JSONObject replyitem = jsonArray1.getJSONObject(i);
String replychatid = replyitem.getString("chatid");
Log.d(TAG,"yong parsejsonuserhistory replychatid "+replychatid);
}

Look!!
------解决思路----------------------
引用:
for( int j=0;j<jsonArray1.length();j++ ) {
JSONObject replyitem = jsonArray1.getJSONObject(i);
String replychatid = replyitem.getString("chatid");
Log.d(TAG,"yong parsejsonuserhistory replychatid "+replychatid);
}

Look!!


好眼力
------解决思路----------------------
2楼好牛逼
  相关解决方案