如何使用多个数组对象访问JSON数据:android

时间:2016-11-26 10:41:28

标签: java android json

我被困在从多个数据集的JSON文件中获取数据。

{"status":"ok","count":3,"count_total":661,"pages":133,"posts": 
[{"id":20038,"type":"post","slug":"xperia-launcher-download","url":"http:\/\/missingtricks.net\/xperia-launcher-download\/","status":"publish","title":"Download Xperia Launcher app for Android (Latest Version)",
{"id":94,"type":"post","slug":"top-free-calling-apps-of-2014-year","url":"http:\/\/missingtricks.net\/top-free-calling-apps-of-2014-year\/","status":"publish","title":"Best Free Calling Apps for Android November 2014",
{"id":98,"type":"post","slug":"top-free-calling-apps-of-2016-year" "url":"http:\/\/missingtricks.net\/top-free-calling-apps-of-2016-year\/","status":"publish","title":"Best Free Calling Apps for Android December 2016"}]}

我需要从上面的JSON文件中访问标题,网址和状态。

@Override
    protected void onPostExecute(String result) {
        //this method will be running on UI thread

        pdLoading.dismiss();
        List<DataFish> data = new ArrayList<>();
        pdLoading.dismiss();
        try {
            JSONArray jArray = new JSONArray(result);
            // Extract data from json and store into ArrayList as class objects
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                DataFish fishData = new DataFish();
                fishData.status = json_data.getString("status");                 
                fishData.title = json_data.getString("url");                  
                fishData.sizeName = json_data.getString("title");
                data.add(fishData);
            }
        } catch (JSONException e) {
            Toast.makeText(JSonActivity.this, e.toString(), Toast.LENGTH_LONG).show();
            Log.d("Json","Exception = "+e.toString());
        }
    }

我收到了上面代码的JSONException。

如何从JSON文件中访问标题,状态和网址?

2 个答案:

答案 0 :(得分:2)

您必须抓取JSONArray内的JSONObject,因此请创建JSONObject并使用索引&#34;帖子&#34;

获取您的数组

1。)resultJSONObject所以创建JSONObject

2。)获取您的JSONArray索引值为&#34;帖子&#34;

3。)现在只需通过索引

获取数组对象即可
        JSONObject jObj = new JSONObject(result);
        JSONArray jArray = jObj.getJSONArray("posts");

        // Extract data from json and store into ArrayList as class objects
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject json_data = jArray.getJSONObject(i);


            DataFish fishData = new DataFish();
            fishData.status = json_data.getString("status");

            fishData.title = json_data.getString("url");

            fishData.sizeName = json_data.getString("title");


            data.add(fishData);
        }

注意:我不知道天气这是一个带有较短版本的示例回复,尽管您的json对象应以}而不是,结尾。

  

[{&#34; ID&#34;:20038&#34;类型&#34;:&#34;后&#34;&#34;蛞蝓&#34;:&#34; XPERIA-发射-download&#34;&#34; URL&#34;:&#34; HTTP://missingtricks.net/xperia-launcher-download/",&#34;状态&#34;:&#34 ;发布&#34;&#34;标题&#34;:&#34;下载   适用于Android的Xperia Launcher应用程序(最新版本)&#34;,

//  ^^^ there should be a } not a , to end json
// so make sure to do the correction so it will look like =>  ...st Version)"}, 
  

{&#34; ID&#34;:94,&#34;类型&#34;:&#34;后&#34;&#34;蛞蝓&#34;:&#34;顶部自由主叫应用-的2014年&#34;&#34;网址&#34;:&#34; HTTP://missingtricks.net/top-free-calling-apps-of-2014-year/&# 34;,&#34;状态&#34;:&#34;发布&#34;&#34;标题&#34;:&#34;最佳   适用于Android的免费通话应用程序2014年11月和#34;,]

改进:

如果没有映射键,您可以使用optString来避免空值或非字符串值

这有两个变体

  

获取与密钥关联的可选字符串。它返回   如果没有这样的密钥,则为defaultValue。

 public String optString(String key, String defaultValue) {
  fishData.status = json_data.optString("status","N/A");
 // will return "N/A" if no key found 

或者如果找不到密钥则获取空字符串,然后只需使用

  fishData.status = json_data.optString("status");
 // will return "" if no key found where "" is an empty string

答案 1 :(得分:0)

您可以验证您的JSON here

如果整个JSON getJsonObject()不起作用,那么你应该解析JSON对象&amp;多个数组中的数组然后使用它们。