简体   繁体   English

当试图从JSONArray中检索JSONObject时返回null?

[英]When trying to retrieve JSONObject out of JSONArray is returning null?

Im trying to do the following: 我试着做以下事情:

UserFunctions uf = new UserFunctions();
    JSONArray json = uf.getAllFreebies();
    System.out.println(json + "blah1"); //Here im able to retrieve the whole JSONArray.

try{    
System.out.println("1");
    for (int i = 1; i <json.length(); i++) {
        System.out.println("2");
            jo = json.optJSONObject(i); //Im getting null value here
            System.out.println(jo + "blah2"); //Here im getting null

...} ...}

Im printing out my JSONArray using following lines. 我使用以下行打印出我的JSONArray。

JSONArray json = uf.getAllFreebies();
System.out.println(json + "blah1"); 

Now on the following lines when i tried to retrieve the JSONObject from the JSONArray and tried to print it out,it prints out "null" instead of the JSONObject at the particular index. 现在,在以下几行中,当我尝试从JSONArray检索JSONObject并尝试将其打印出来时,它会打印出“null”而不是特定索引处的JSONObject。

jo = json.optJSONObject(i);
System.out.println(jo + "blah2"); 

Can anyone pls tell me what am i doing wrong?I mean how can i get a null for JSONOBject when my JSONArray is not null? 任何人都可以告诉我,我做错了什么?我的意思是当我的JSONArray不为空时,如何为JSONOBject获取null?

Thank You. 谢谢。

Following is my JSONArray logcat: 以下是我的JSONArray logcat:

05-31 21:02:57.156: I/System.out(318): [["viking","Art","Potrait","Potrait","Good","im giving out potrait 7325697","176 Fiat Ave","Iselin","New Jersey","USA","08830","2012-05-27"],["n00b","Books","Novels","Novels","Good","Im giving out novels 7325697","b9 maa krupa","petlad","Gujarat","India","388450","2012-05-27"],["n00b","Computers","laptop","laptop giveaway","Good","Im giving out laptop if you are interested than pls call on 7325697","B9 Ma  Krupa","Petlad","Gujarat","India","388450","2012-05-27"],["mista","Cameras & Photos","Camera","Camera GiveAway","Very good","im giving out camera .its kodak .pls email me on mista@gmail.com","Mista Lee elm street","seoul","Korea","South Korea","ha8 9sd","2012-05-27"],["panda","Gaming Consoles","XBOX","XBOX 360","Very Good","Im giving out xbox 360.if you are interested please email me on panda@gmail.com","435 Carmen Rd,","Buffalo","New York","USA","14226","2012-05-27"],["viking","Cameras & Photos","Camera","Kodak Camera","Good","Kodak Camera giveaway.Pls call on 732397","","Iselin","New Jersey","USA","08830","2012-05-28"],["viking","Books","Novels","Novel GA","Good","Novel give away.call on 7325697.","","Iselin","New Jersey","USA","08830","2012-05-28"],["viking","Automotive","Car","Car GiveAway","Good","Im giving out car.if you are interested pls call 7323697.","176 Fiat Ave","Iselin","New Jersey","USA","08830","2012-05-29"],["viking","Collectibles","Medallions","Medallion GA","Very Good","Im giving out medallion.if inetrested pls call 732697","176 Fiat Ave,","Iselin","New Jersey","USA","08830","2012-05-29"],["viking","Clothing & Accessories","cloths","cloths giveaway","Good","im giving out cloths if you are interested pls call on 735697","176 Fiat Ave,","Iselin","New jersey","USA","08830","2012-05-29"],["viking","Books","Novel","Novel GA","Good","pls call 735697","435 carmen rd","buffalo","ny","usa","14226","2012-05-29"],["viking","Books","TextBook","CHemistry 101","GOod","pls call 735697","176 fiat ave","iselin","new jersey","usa","08830","2012-05-29"],["mista","Books","Notebook","Notbook","Good","im giving out notebbok if you are interested pls call 48374288423","elm street","seaoul","na","South Korea","jfjafjk","2012-05-29"]]blah1

logcat output when trying to print out JSONOBject at index i 尝试在索引i处打印出JSONOBject时的logcat输出

05-31 21:02:57.156: I/System.out(318): nullblah2

The JSON [[...], [...], [...]] does not contain JSON-objects, but it does have some JSON-arrays. JSON [[...], [...], [...]]不包含JSON对象,但它确实有一些JSON数组。

Thus optJSONObject finds a JSON-array (where it expected a JSON-object) and returns null because it is of the incorrect type. 因此, optJSONObject找到一个JSON数组(它期望一个JSON对象)并返回null因为它的类型不正确。 ( opt is short for optional .) opt可选的缩写。)

Use optJSONArray (note Array and not Object ). 使用optJSONArray (注意Array而不是Object )。 Alternatively, use getJSONArray , which will throw a JSONException on failure. 或者,使用getJSONArray ,它会在失败时抛出JSONException。

Happy coding. 快乐的编码。


Note that JSON keeps a strict distinction between Objects, Arrays, and the various Values. 请注意,JSON严格区分对象,数组和各种值。 The concept of an Array being a special (sub)kind of Object (in the JSON sense) exists in JavaScript but does not necessarily extend to other JSON implementations. 数组是一个特殊的(子)类型的对象(在JSON的意义上)的概念存在于JavaScript,但不一定扩展到其他JSON实现。 For instance, the JSONArray class has no relationship with the JSONObject class. 例如, JSONArray类与JSONObject类没有任何关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM