简体   繁体   中英

Parsing multiple JsonObject and JsonArray

I have some issues with using multiple jsonobjects I want to use "posts" and "attachments" jsonobjects.

but I tried to use the line and another for loop for attachments jsonObject but it doesnt work.

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

My Json looks like this:

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

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

Java code:

    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);

        }


    }
}
}

The type related to 'attachments' is an array, therefore you should call something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM