如何解析大量不同值的嵌套jsonobjects?

How to parse a large number of nested json objects with different values?

这是一个 link:http://mobevo.ext.terrhq.ru/shr/j/ru/technology.js 和 JSON object。有 261 个 object 具有唯一值(字符串)。如何获取每个 object 带有数字(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);
    }
}

获得 "technology" 对象后,只需创建另一个 JSON 对象,如下所示:

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 看起来可能更适合使用 JSON 数组而不是嵌套的 JSON 对象。

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

   //Action need to be performed
}

您可以将所有名称获取到 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;
    }

}