JSONObject不会解析HTTP Post响应中的所有数据

时间:2012-05-15 11:47:18

标签: java android json

我正面临一个让我发疯的问题,我在这个网站和谷歌搜索并找不到任何结果。

我正在使用使用JSON解析器的android sdk构建应用程序。

我编写了处理http帖子的代码,我得到了正确的响应,但是当我尝试使用以下方法将其解析为JSONObject时:

strJson - >来自服务器的响应。

JSONobject json = new JSONObject(strJson)

它没有解析所有数据,有些对象是空白的(空指针)

我用许多工具(包括jsonlint.com)及其有效的

检查了json响应

问题是什么?

BTW这是json的回应:

{
"tag": "login",
"success": 1,
"error": 0,
"userprofile": {
    "0": "123",
    "1": "0",
    "2": "mock@g.com",
    "3": "e807f1fcf82d132f9bb018ca6738a19f",
    "4": "misha",
    "5": "user",
    "6": "2000-05-22",
    "7": "264",
    "8": "Ben Gurion, Engineer",
    "9": "female",
    "10": "female",
    "11": "unemployed",
    "12": "Sillicon Vally, USA",
    "13": "english",
    "14": "hi i am new to heyou",
    "15": "1",
    "16": "2012-04-15 11:11:29",
    "user_id": "123",
    "facebook_id": "0",
    "email": "mock@g.com",
    "password": "e807f1fcf82d132f9bb018ca6738a19f",
    "firstname": "misha",
    "lastname": "user",
    "dob": "2000-05-22",
    "points": "264",
    "study": "Ben Gurion, Engineer",
    "gender": "female",
    "intrested": "female",
    "profession": "unemployed",
    "location": "Sillicon Vally, USA",
    "knows": "english",
    "status": "hi i am new to heyou",
    "activated": "1",
    "created": "2012-04-15 11:11:29"
},
"userpictures": {
    "0": {
        "0": "1",
        "1": "123",
        "2": "profile",
        "3": "http://profile.ak.fbcdn.net/hprofile-ak-ash2/372237_100000725612104_1712537362_n.jpg",
        "4": "2012-04-23 01:51:15",
        "node_id": "1",
        "user_id": "123",
        "type": "profile",
        "path": "http://profile.ak.fbcdn.net/hprofile-ak-ash2/372237_100000725612104_1712537362_n.jpg",
        "updated": "2012-04-23 01:51:15"
    },
    "tag": "getpictures",
    "success": 0,
    "error": 0,
    "user_id": "123",
    "numberofpictures": 1
},
"userfriends": {
    "0": {
        "user_id": "456",
        "name": "aksjdkasjdkasjdk ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/369825_1446671611_369000795_n.jpg",
        "online": "1",
        "longitude": "32.5568774",
        "lawtitude": "34.8789575",
        "address": "Israel ,Beersheva ,even susan",
        "client": "android"
    },
    "1": {
        "user_id": "789",
        "name": "naty ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/368727_1016960715_915310545_n.jpg",
        "online": "0",
        "longitude": "32.58964587",
        "lawtitude": "38.54578998",
        "address": "IRAQ ,Bagdad ,bag 9288",
        "client": "android"
    },
    "2": {
        "user_id": "4fa01f9e1d9243.41584266",
        "name": "matan ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-ash2/372237_100000725612104_1712537362_n.jpg",
        "online": "",
        "longitude": "",
        "lawtitude": "",
        "address": "",
        "client": ""
    },
    "3": {
        "user_id": "456",
        "name": "aksjdkasjdkasjdk ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/369825_1446671611_369000795_n.jpg",
        "online": "1",
        "longitude": "32.5568774",
        "lawtitude": "34.8789575",
        "address": "Israel ,Beersheva ,even susan",
        "client": "android"
    },
    "4": {
        "user_id": "789",
        "name": "naty ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/368727_1016960715_915310545_n.jpg",
        "online": "0",
        "longitude": "32.58964587",
        "lawtitude": "38.54578998",
        "address": "IRAQ ,Bagdad ,bag 9288",
        "client": "android"
    },
    "5": {
        "user_id": "789",
        "name": "naty ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/368727_1016960715_915310545_n.jpg",
        "online": "0",
        "longitude": "32.58964587",
        "lawtitude": "38.54578998",
        "address": "IRAQ ,Bagdad ,bag 9288",
        "client": "android"
    },
    "6": {
        "user_id": "456",
        "name": "aksjdkasjdkasjdk ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/369825_1446671611_369000795_n.jpg",
        "online": "1",
        "longitude": "32.5568774",
        "lawtitude": "34.8789575",
        "address": "Israel ,Beersheva ,even susan",
        "client": "android"
    },
    "7": {
        "user_id": "456",
        "name": "aksjdkasjdkasjdk ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/369825_1446671611_369000795_n.jpg",
        "online": "1",
        "longitude": "32.5568774",
        "lawtitude": "34.8789575",
        "address": "Israel ,Beersheva ,even susan",
        "client": "android"
    },
    "8": {
        "user_id": "789",
        "name": "naty ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/368727_1016960715_915310545_n.jpg",
        "online": "0",
        "longitude": "32.58964587",
        "lawtitude": "38.54578998",
        "address": "IRAQ ,Bagdad ,bag 9288",
        "client": "android"
    },
    "9": {
        "user_id": "456",
        "name": "aksjdkasjdkasjdk ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/369825_1446671611_369000795_n.jpg",
        "online": "1",
        "longitude": "32.5568774",
        "lawtitude": "34.8789575",
        "address": "Israel ,Beersheva ,even susan",
        "client": "android"
    },
    "10": {
        "user_id": "123",
        "name": "misha ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-ash2/372237_100000725612104_1712537362_n.jpg",
        "online": "0",
        "longitude": "23",
        "lawtitude": "54",
        "address": " ,asdkasjd ,asdasd",
        "client": "android"
    },
    "11": {
        "user_id": "789",
        "name": "naty ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/368727_1016960715_915310545_n.jpg",
        "online": "0",
        "longitude": "32.58964587",
        "lawtitude": "38.54578998",
        "address": "IRAQ ,Bagdad ,bag 9288",
        "client": "android"
    },
    "12": {
        "user_id": "789",
        "name": "naty ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/368727_1016960715_915310545_n.jpg",
        "online": "0",
        "longitude": "32.58964587",
        "lawtitude": "38.54578998",
        "address": "IRAQ ,Bagdad ,bag 9288",
        "client": "android"
    },
    "13": {
        "user_id": "789",
        "name": "naty ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/368727_1016960715_915310545_n.jpg",
        "online": "0",
        "longitude": "32.58964587",
        "lawtitude": "38.54578998",
        "address": "IRAQ ,Bagdad ,bag 9288",
        "client": "android"
    },
    "14": {
        "user_id": "456",
        "name": "aksjdkasjdkasjdk ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/369825_1446671611_369000795_n.jpg",
        "online": "1",
        "longitude": "32.5568774",
        "lawtitude": "34.8789575",
        "address": "Israel ,Beersheva ,even susan",
        "client": "android"
    },
    "15": {
        "user_id": "123",
        "name": "misha ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-ash2/372237_100000725612104_1712537362_n.jpg",
        "online": "0",
        "longitude": "23",
        "lawtitude": "54",
        "address": " ,asdkasjd ,asdasd",
        "client": "android"
    },
    "16": {
        "user_id": "789",
        "name": "naty ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/368727_1016960715_915310545_n.jpg",
        "online": "0",
        "longitude": "32.58964587",
        "lawtitude": "38.54578998",
        "address": "IRAQ ,Bagdad ,bag 9288",
        "client": "android"
    },
    "17": {
        "user_id": "789",
        "name": "naty ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/368727_1016960715_915310545_n.jpg",
        "online": "0",
        "longitude": "32.58964587",
        "lawtitude": "38.54578998",
        "address": "IRAQ ,Bagdad ,bag 9288",
        "client": "android"
    },
    "18": {
        "user_id": "789",
        "name": "naty ",
        "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/368727_1016960715_915310545_n.jpg",
        "online": "0",
        "longitude": "32.58964587",
        "lawtitude": "38.54578998",
        "address": "IRAQ ,Bagdad ,bag 9288",
        "client": "android"
    },
    "tag": "getfriendlist",
    "success": 1,
    "user_id": "123",
    "numberoffriends": 19
},
"usergoods": {
    "0": {
        "0": "123",
        "1": "Burger",
        "2": "Eat some burger",
        "3": "Gifts",
        "4": "25",
        "5": "25",
        "6": "http://www.pythagorapps.com/heyou/gifts/123.png",
        "7": null,
        "8": "2012-05-07 01:25:53",
        "product_id": "123",
        "product_name": "Burger",
        "product_desc": "Eat some burger",
        "category": "Gifts",
        "product_price": "25",
        "product_points": "25",
        "path": "http://www.pythagorapps.com/heyou/gifts/123.png",
        "locale": null,
        "created": "2012-05-07 01:25:53"
    },
    "1": {
        "0": "456",
        "1": "Beer",
        "2": "Drink ice cold beer",
        "3": "Gifts",
        "4": "30",
        "5": "30",
        "6": "http://www.pythagorapps.com/heyou/gifts/456.png",
        "7": null,
        "8": "2012-05-07 01:25:53",
        "product_id": "456",
        "product_name": "Beer",
        "product_desc": "Drink ice cold beer",
        "category": "Gifts",
        "product_price": "30",
        "product_points": "30",
        "path": "http://www.pythagorapps.com/heyou/gifts/456.png",
        "locale": null,
        "created": "2012-05-07 01:25:53"
    },
    "2": {
        "0": "789",
        "1": "Coctail",
        "2": "Drink a refreshing coctail",
        "3": "Gifts",
        "4": "50",
        "5": "50",
        "6": "http://www.pythagorapps.com/heyou/gifts/789.png",
        "7": null,
        "8": "2012-05-10 04:56:18",
        "product_id": "789",
        "product_name": "Coctail",
        "product_desc": "Drink a refreshing coctail",
        "category": "Gifts",
        "product_price": "50",
        "product_points": "50",
        "path": "http://www.pythagorapps.com/heyou/gifts/789.png",
        "locale": null,
        "created": "2012-05-10 04:56:18"
    },
    "tag": "getgoods",
    "success": 1,
    "error": 0,
    "numofgoods": 3
},
"useravatar": {
    "tag": "getlatest",
    "success": 0,
    "error": 10,
    "err_msg": "cannot connect to mySQL database"
},
"shopcategories": {
    "0": {
        "0": "1",
        "1": "Shirts",
        "2": "Buy some cool new shirts",
        "3": "null",
        "4": "2012-05-07 01:09:02",
        "index": "1",
        "name": "Shirts",
        "desc": "Buy some cool new shirts",
        "path": "null",
        "created": "2012-05-07 01:09:02"
    },
    "1": {
        "0": "2",
        "1": "Gifts",
        "2": "buy some cool gifts",
        "3": "null",
        "4": "2012-05-07 01:09:58",
        "index": "2",
        "name": "Gifts",
        "desc": "buy some cool gifts",
        "path": "null",
        "created": "2012-05-07 01:09:58"
    },
    "tag": "getcat",
    "success": 1,
    "error": 0,
    "numofcat": 2
},
"shopproducts": {
    "0": {
        "0": "123",
        "1": "Burger",
        "2": "Eat some burger",
        "3": "Gifts",
        "4": "25",
        "5": "25",
        "6": "http://www.pythagorapps.com/heyou/gifts/123.png",
        "7": null,
        "8": "2012-05-07 01:25:53",
        "product_id": "123",
        "product_name": "Burger",
        "product_desc": "Eat some burger",
        "category": "Gifts",
        "product_price": "25",
        "product_points": "25",
        "path": "http://www.pythagorapps.com/heyou/gifts/123.png",
        "locale": null,
        "created": "2012-05-07 01:25:53"
    },
    "1": {
        "0": "456",
        "1": "Beer",
        "2": "Drink ice cold beer",
        "3": "Gifts",
        "4": "30",
        "5": "30",
        "6": "http://www.pythagorapps.com/heyou/gifts/456.png",
        "7": null,
        "8": "2012-05-07 01:25:53",
        "product_id": "456",
        "product_name": "Beer",
        "product_desc": "Drink ice cold beer",
        "category": "Gifts",
        "product_price": "30",
        "product_points": "30",
        "path": "http://www.pythagorapps.com/heyou/gifts/456.png",
        "locale": null,
        "created": "2012-05-07 01:25:53"
    },
    "2": {
        "0": "789",
        "1": "Coctail",
        "2": "Drink a refreshing coctail",
        "3": "Gifts",
        "4": "50",
        "5": "50",
        "6": "http://www.pythagorapps.com/heyou/gifts/789.png",
        "7": null,
        "8": "2012-05-10 04:56:18",
        "product_id": "789",
        "product_name": "Coctail",
        "product_desc": "Drink a refreshing coctail",
        "category": "Gifts",
        "product_price": "50",
        "product_points": "50",
        "path": "http://www.pythagorapps.com/heyou/gifts/789.png",
        "locale": null,
        "created": "2012-05-10 04:56:18"
    },
    "tag": "getall",
    "success": 1,
    "error": 0,
    "numofproducts": 3,
    "user_id": "123"
}
}

BTW 2:

iOS应用似乎正确解析它。

提前感谢。

编辑:

获得回复的代码是:

    public JSONObject getJSONFromURL(String URL,List<NameValuePair> params){

    //try to make http request:

    try{
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(URL+listToString(params));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    }
    catch(UnsupportedEncodingException e){
        e.printStackTrace();
    }
    catch(ClientProtocolException e){
        e.printStackTrace();
    }
    catch(IOException e){
        e.printStackTrace();
    }


    //try to read the respond:

    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line = reader.readLine())!=null){
            sb.append(line+"\n");
        }
        is.close();
        strJson = sb.toString();
        Log.e("JSON",strJson);
    }
    catch(Exception e){
        Log.e("Buffer Error","Error converting result " + e.toString());
    }

    //try to parse the string to JSON object:
    try{
        json = new JSONObject(strJson);
    }
    catch(JSONException e){
        Log.e("JSON Parser","Error parsing data " + e.toString());
    }

    return json;

}

json返回的json对象缺少一些像userprofiles,success等对象......

但strJson有效!

1 个答案:

答案 0 :(得分:0)

  
    

String value =(String)jsonserch2.opt(key);

  

当对象ha为null时,请尝试此操作,它会自动拒绝它 而是 jsonserch2.getString(key);