0

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

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

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

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

JSONobject json = 新 JSONObject(strJson)

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

我使用许多工具(包括 jsonlint.com)检查了 json 响应及其有效

问题是什么?

顺便说一句,这是 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"
}
}

顺便说一句 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 对象缺少一些对象,如用户配置文件、成功等...

但 strJson 是有效的!

4

1 回答 1

0

字符串值 = (String) jsonserch2.opt(key);

当对象具有空值时尝试此操作,它会自动拒绝它而不是jsonserch2.getString(key) ;

于 2012-05-15T12:21:52.380 回答