如何用不同的值解析大量嵌套的json对象?

时间:2015-04-10 17:19:33

标签: java android json parsing jsonobject

这是一个带有JSON对象的链接:http://mobevo.ext.terrhq.ru/shr/j/ru/technology.js。有261个具有唯一值(字符串)的对象。如何用数字(2101,2107等)和2个字符串(图片和标题)获取每个对象?

所以这是我的技术AsyncTask:

ListView listView;
TechnologiesAdapter adapter;
ArrayList<Technologies> techList;

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

    listView = (ListView) findViewById(R.id.listView);
    techList = new ArrayList<Technologies>();

    new TechnologiesAsyncTask().execute("http://mobevo.ext.terrhq.ru/shr/j/ru/technology.js");
}

public class TechnologiesAsyncTask extends AsyncTask<String, Void, Boolean> {

    @Override
    protected Boolean doInBackground(String... urls) {
        try {
            HttpGet httppost = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);


                JSONObject jObj1 = new JSONObject(data);
                JSONObject jObj2 = jObj1.getJSONObject("technology");

                //How to get the other objects?

                return true;
            }

        } catch (ParseException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }



    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
    }
}

3 个答案:

答案 0 :(得分:0)

获得&#34;技术&#34; object只需创建另一个JSONObject,如下所示:

HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);

JSONObject jObj1 = new JSONObject(data);
JSONObject jObj2 = jObj1.getJSONObject("technology");

// Get the 2101 object
JSONObject jObj3 = jObj2.getJSONObject("2101");
// Get the picture and title of 2101
String picture = jObj3.getString("picture");
String title = jObj3.getString("title");

您的JSON看起来可能更适合使用JSONArray而不是嵌套的JSONObjects。

答案 1 :(得分:0)

JSONObject jPages=jQuery.getJSONObject("pages");
Iterator < String> keys = jPages.keys();
while(keys.hasNext()){
   JSONObject jPageId=jPages.getJSONObject(keys.next());

   //Action need to be performed
}

答案 2 :(得分:0)

您可以将所有名称都添加到json数组中,然后从json数组中的名称中获取每个json对象。

以下是一个简单的例子。

private class DataAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        URL url;
        try {
            url = new URL("http://mobevo.ext.terrhq.ru/shr/j/ru/technology.js");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            StringBuilder builder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }

            JSONObject jsonObject = new JSONObject(builder.toString()).getJSONObject("technology");
            JSONArray nameArray = jsonObject.names();
            final int size = nameArray.length();
            for (int i = 0; i < size; i++) {
                JSONObject object = jsonObject.getJSONObject(nameArray.getString(i));
                // get id, title and pictures, etc
                Log.d(TAG, nameArray.getString(i) + " " + object.getString("title") + " " + object.getString("picture"));
            }

        } catch (IOException | JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

}