我正在尝试执行以下操作:
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获取null?
谢谢。
以下是我的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
答案 0 :(得分:4)
JSON [[...], [...], [...]]
不包含JSON对象,但它确实有一些JSON数组。
因此optJSONObject
找到一个JSON数组(它预期一个JSON对象)并返回null
,因为它的类型不正确。 (opt
是可选的缩写。)
使用optJSONArray
(注意数组而非对象)。或者,使用getJSONArray
,这将在失败时抛出JSONException。
快乐的编码。
请注意,JSON严格区分对象,数组和各种值。作为特殊(子)类Object(在JSON意义上)的Array的概念存在于JavaScript中,但不必然扩展到其他JSON实现。例如,JSONArray类与JSONObject类具有 no 关系。