0

我需要用多个数组解析 JSON 文件。数组格式为:

{
    "List": {
        "Something": [
            {
                "Name": "John",
                "phone": "test"
            }
        ]
        "SomethingElse": [
            {
                "Name": "Smith",
                "phone": "test"
            }
        ]
    }
}

问题是我不知道下一个数组名称是什么。是否可以解析所有数组中的数据而无需名称且不更改其结构?

谢谢。

4

3 回答 3

1

JSON

{
  "data": {
    "current_condition": [
      {
        "cloudcover": "25",
        "humidity": "62",
        "observation_time": "04:57 PM",
        "precipMM": "0.1",
        "pressure": "1025",
        "temp_C": "10",
        "temp_F": "50",
        "visibility": "10",
        "weatherCode": "113",
        "weatherDesc": [
          {
            "value": "Clear"
          }
        ]
      }
    ]
  }
}

获取“weatherDesc”的价值

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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(MainActivity.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Android JSON Parse Tutorial");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Create an array
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrieve JSON Objects from the given URL address
        jsonobject = JSONfunctions
                .getJSONfromURL("http://api.worldweatheronline.com/free/v1/weather.ashx?q=London&format=json&num_of_days=5&key=8mqumbup9fub7bcjtsxbzxx9");

        try {
            // Locate the array name in JSON
            JSONObject on=jsonobject.getJSONObject("data");
            jsonarray = on.getJSONArray("current_condition");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
            JSONArray sen1=jsonobject.getJSONArray("weatherDesc");
                // Retrive JSON Objects
            for(int j=0;j<sen1.length();j++){
                jsonobject2 = sen1.getJSONObject(j);
                map.put("value0", jsonobject2.getString("value"));
                map.put("value1",jsonobject2.getString("value"));

                map.put("flag", null);
                // Set the JSON Objects into the array
                arraylist.add(map);
            }
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void args) {
        // Locate the listview in listview_main.xml
        listview = (ListView) findViewById(R.id.listview);
        // Pass the results into ListViewAdapter.java
        adapter = new ListViewAdapter(MainActivity.this, arraylist);
        // Set the adapter to the ListView
        listview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
    }
}
于 2013-11-14T19:52:15.260 回答
0

尝试这个

我编写了一个函数来递归地迭代 JsonObject 而不知道键名。

private void parseJson(JSONObject data) {

        if (data != null) {
            Iterator<String> it = data.keys();
            while (it.hasNext()) {
                String key = it.next();

                try {
                    if (data.get(key) instanceof JSONArray) {
                        JSONArray arry = data.getJSONArray(key);
                        int size = arry.length();
                        for (int i = 0; i < size; i++) {
                            parseJson(arry.getJSONObject(i));
                        }
                    } else if (data.get(key) instanceof JSONObject) {
                        parseJson(data.getJSONObject(key));
                    } else {
                        System.out.println("" + key + " : " + data.optString(key));
                    }
                } catch (Throwable e) {
                    System.out.println("" + key + " : " + data.optString(key));
                    e.printStackTrace();

                }
            }
        }
    }
于 2013-08-24T08:56:44.820 回答
-1

JSON 中的值后面缺少一个逗号Something,但除此之外,您可以使用任何 JSON 解析器对其进行解析。

于 2013-08-24T08:02:21.753 回答