2012-05-15 27 views
0

我面临一个让我疯狂的问题,我在这个网站和谷歌搜索,并找不到任何结果。JSONObject不会解析来自HTTP的所有数据Post response

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

我写的处理HTTP POST和我得到正确的响应,但是当我试图把它解析为JSONObject的使用代码:

strJson - >从服务器的响应。

JSONObject的JSON =新的JSONObject(strJson)

它不是分析所有的数据和一些对象是空(NULL指针)

我检查了许多工具(包括jsonlint.com)JSON响应和其有效

有什么问题?

顺便说一句,这是JSON响应:

{ 
"tag": "login", 
"success": 1, 
"error": 0, 
"userprofile": { 
    "0": "123", 
    "1": "0", 
    "2": "[email protected]", 
    "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": "[email protected]", 
    "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,成功等一些物品...

但strJson已验证!

+0

发布您的代码? – jiduvah

+1

启动JSONArrays时不会缺少“[”“]? – goodm

+0

是否行>>“user_id”:“4fa01f9e1d9243.41584266”,<<导致你的一些问题?您所有其他的'user_id'字段都是三位十进制数字。 – rossum

回答

0

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

尝试此当对象公顷空值,它会自动拒绝它 代替jsonserch2.getString(键);

+0

得到一个特定的键值没有问题,问题是整个“userprofile”对象丢失,insted它有NULL指针 – user1396033

+0

感谢您的回复, 但问题是对象的名称。 .. 我想他们太长了 将“userprofile”更改为“profile”解决了问题,现在Android json解析器的工作方式就像魅力一样。 考虑到iOS和许多其他验证工具没有遇到这个问题,这是有线的。 – user1396033