解析多个JsonObject和JsonArray

时间:2016-08-18 22:17:28

标签: java android json android-studio

我在使用多个jsonobjects时遇到一些问题我想使用“posts”和“attachments”jsonobjects。

但我尝试使用该行和另一个用于附件jsonObject的循环,但它不起作用。

    String postInfo = jsonObject.getString("attachments");  

我的Json看起来像这样:

{"posts":[
       {"title":"Title","content":"Post content"}

     ]
  }
    {"attachments":[
       {"url":"http://www.something.com"}
     ]
    }

Java代码:

    public class NewsActivity extends FragmentActivity {
  ViewPager viewPager;
 int category;
ArrayList titleList;
ArrayList postList;
ArrayList imgList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news);

    Intent i = getIntent();
    category=i.getIntExtra("locationInfo",-1);

    try {
        String encodedCatName = URLEncoder.encode(Integer.toString(category), "UTF-8");

        DownloadTask task = new DownloadTask();
        task.execute("http://www.something.co/api/get_category_posts/?id=" + encodedCatName);


    } catch (UnsupportedEncodingException e) {

        e.printStackTrace();

        // Toast.makeText(getApplicationContext(), "Could not find weather", Toast.LENGTH_LONG);

    }


}

public class DownloadTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... urls) {
        postList = new ArrayList();
        titleList = new ArrayList();
        imgList = new ArrayList();
        String result = "";
        URL url;
        HttpURLConnection urlConnection = null;

        try {
            url = new URL(urls[0]);

            urlConnection = (HttpURLConnection) url.openConnection();

            InputStream in = urlConnection.getInputStream();

            InputStreamReader reader = new InputStreamReader(in);

            int data = reader.read();

            while (data != -1) {

                char current = (char) data;

                result += current;

                data = reader.read();

            }

            return result;

        } catch (Exception e) {

            Toast.makeText(getApplicationContext(), "Could not find", Toast.LENGTH_LONG);

        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);


        try {

            String message = "";

            JSONObject jsonObject = new JSONObject(result);

            String postInfo = jsonObject.getString("posts");

            Log.i("Content", postInfo);

            JSONArray arr = new JSONArray(postInfo);
            JSONArray attachments = jsonObject.getJSONArray("attachments");

            for(int i=0; i< attachments.length(); i++){
                String url = "";
                url = attachments.getJSONObject(i).getString("url");
                imgList.add(url);
            }

            for (int i = 0; i < arr.length(); i++) {

                JSONObject jsonPart = arr.getJSONObject(i);

                String title = "";
                String post = "";

                title = jsonPart.getString("title");
                post = jsonPart.getString("content");


                if (title != "" && post != "") {

                    message += title + ": " + post + "\r\n";

                    titleList.add(title);
                    postList.add(post);


                }

            }


                viewPager = (ViewPager) findViewById(R.id.view_pager);
                SwipeAdapter swipeAdapter = new SwipeAdapter(getSupportFragmentManager(),category,titleList,postList,imgList);
                viewPager.setAdapter(swipeAdapter);



        } catch (JSONException e) {

            Toast.makeText(getApplicationContext(), "Could not find ", Toast.LENGTH_LONG);

        }


    }
}
}

1 个答案:

答案 0 :(得分:0)

与&#39;附件相关的类型&#39;是一个数组,因此你应该调用类似的东西:

JSONArray attachments = jsonObject.getJSONArray("attachments")
for(int i=0; i< attachments.length(); i++){
  attachments.getJSONObject(i).getString("url");
}
相关问题