【问题标题】:Parsing multiple JsonObject and JsonArray解析多个 JsonObject 和 JsonArray
【发布时间】:2016-08-18 22:17:28
【问题描述】:

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

但我尝试使用该行和另一个 for 循环作为附件 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);

        }


    }
}
}

【问题讨论】:

    标签: java android json android-studio


    【解决方案1】:

    'attachments' 相关的类型是一个数组,因此你应该这样调用:

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

    【讨论】:

    • 我试过了,但它不起作用,我在问题上编辑了 java 代码,你能再看一次吗,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多