1

我正在尝试执行以下操作:

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

...}

我使用以下几行打印出我的 JSONArray。

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

现在在以下几行中,当我尝试从 JSONArray 检索 JSONObject 并尝试将其打印出来时,它会在特定索引处打印出“null”而不是 JSONObject。

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

谁能告诉我我做错了什么?我的意思是当我的 JSONArray 不为空时,我如何为 JSONOBject 获得空值?

谢谢你。

以下是我的 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

尝试在索引 i 处打印 JSONOBject 时的 logcat 输出

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

1 回答 1

4

JSON[[...], [...], [...]]不包含 JSON 对象,但它确实有一些 JSON 数组。

因此optJSONObject找到一个 JSON 数组(它需要一个 JSON 对象)并返回null,因为它的类型不正确。(opt可选的缩写。)

使用optJSONArray(注意Array而不是Object)。或者,使用getJSONArray,这将在失败时抛出 JSONException。

快乐编码。


请注意,JSON 严格区分对象、数组和各种值。Array 是一种特殊(子)类型的 Object(在 JSON 意义上)的概念存在于 JavaScript 中,但不一定扩展到其他 JSON 实现。例如,JSONArray类与 JSONObject 类没有关系。

于 2012-05-31T18:27:22.770 回答