2012-05-15 55 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