2015-07-11 65 views
0

我知道如何解析看起来像[{...},{...},{...}]的JSON。但这里是我的例子,有些不同:android-解析没有显式JSONObject的JSONArray

[{"type":"sometype","presentations":["/files/presentation/presentation.pdf"],"description":"somedescription"},{"type":"sometype","presentations":["/files/presentation/presentation2.pdf"],"description":"somedescription"}] 

如何解析来自“演示文稿”的数据?我得到它作为JSONArray,但不能得到一个值(使用.toString()与该数组返回该值,但与“/”之前“/”(我的意思是“/文件/演示文稿...”) ,所以我不能把它添加到URL来显示PDF我不知道如何从阵列获取的JSONObject(如果存在的话,当然)

+0

你已经尝试过了吗,你可以发布你正在解析的代码吗? – Pankaj

+0

我可以用它来复制粘贴它,但是后来用自然语言来说,我把这个字符串转换成JSONArray,然后从它的数组中提取JSONObject号码,然后做.getString(“演示文稿”),然后将其转换为JSONArray(因为它应该是数组,对吧?)。此外,我做了几件事情:1)我认为这是一个隐式的JSONObject,所以我试图通过数字0获得jSONObject,然后将其转换为字符串,但我记得我甚至无法获得Object。 2)我试图转换为字符串JSONArray本身希望不使用[]和substring后缀(“\ /”)如上所述 –

+0

首先,你可以检查从服务器响应你的json是有几个网站[第一链接](http://jsonformatter.curiousconcept.com/)[第二链接](http://json.parser.online.fr/)。你可以在这里检查你的json是否正确,你可以清楚地看到哪个是正常的字段或对象或数组。 – Pankaj

回答

0
String jsonString = "[{\"type\":\"sometype\",\"presentations\":[\"/files/presentation/presentation.pdf\"],\"description\":\"somedescription\"},{\"type\":\"sometype\",\"presentations\":[\"/files/presentation/presentation2.pdf\"],\"description\":\"somedescription\"}]"; 

try { 
    JSONArray jaArray = new JSONArray(jsonString); 
    JSONObject firstElement = jaArray.getJSONObject(0); 
    JSONArray jaPresentations = firstElement.getJSONArray("presentations"); 
    String string = jaPresentations.getString(0); 

    //or chain it together like so: 
    String string2 = jaArray.getJSONObject(1).getJSONArray("presentations").getString(0); 

    Log.v("TAG", string); 
    Log.v("TAG", string2); 

输出:。

V/TAG﹕ /files/presentation/presentation.pdf 
V/TAG﹕ /files/presentation/presentation2.pdf