1

我想从下面给出的数据中解析一个特定的数组,特别是太阳能:

{
  "Month":
        {"1":"January",
         "2":"February",
         "3":"March",
         "4":"April",
         "5":"May",
         "6":"June",
         "7":"July",
         "8":"August",
         "9":"September",
         "10":"October",
         "11":"November",
         "12":"December"},
  "Air":
        {"1":"26.6",
         "2":"26.9",
         "3":"27.2",
         "4":"27.6",
         "5":"27.6",
         "6":"27.1",
         "7":"26.7",
         "8":"26.5",
         "9":"26.5",
         "10":"26.8",
         "11":"26.7",
         "12":"26.4"},
  "Humid":
        {"1":"79.5%",
         "2":"75.6%",
         "3":"75.8%",
         "4":"75.9%",
         "5":"77.5%",
         "6":"78.2%",
         "7":"79.0%",
         "8":"81.5%",
         "9":"80.5%",
         "10":"77.2%",
         "11":"76.7%",
         "12":"79.7%"},
  "Solar":
        {"1":"5.87",
         "2":"6.51",
         "3":"6.77",
         "4":"6.42",
         "5":"5.51",
         "6":"4.86",
         "7":"4.95",
         "8":"5.09",
         "9":"5.92",
         "10":"6.22",
         "11":"6.12",
         "12":"5.82"},
  "Atm":
        {"1":"101.1",
         "2":"101.1",
         "3":"101.1",
         "4":"101.0",
         "5":"101.0",
         "6":"101.0",
         "7":"101.1",
         "8":"101.1",
         "9":"101.2",
         "10":"101.2",
         "11":"101.2",
         "12":"101.2"},
  "Wind":
        {"1":"3.7",
         "2":"3.9",
         "3":"3.0",
         "4":"2.9",
         "5":"3.7",
         "6":"4.0",
         "7":"3.5",
         "8":"3.7",
         "9":"3.0",
         "10":"3.7",
         "11":"3.9",
         "12":"3.4"},
  "Earth":
        {"1":"28.5",
         "2":"28.6",
         "3":"29.3",
         "4":"30.0",
         "5":"29.9",
         "6":"29.5",
         "7":"29.3",
         "8":"29.0",
         "9":"29.1",
         "10":"28.9",
         "11":"28.5",
         "12":"28.4"},
  "HDD":
        {"1":"0",
         "2":"0",
         "3":"0",
         "4":"0",
         "5":"0",
         "6":"0",
         "7":"0",
         "8":"0",
         "9":"0",
         "10":"0",
         "11":"0",
         "12":"0"},
  "CDD":
        {"1":"514",
         "2":"477",
         "3":"532",
         "4":"529",
         "5":"545",
         "6":"511",
         "7":"516",
         "8":"509",
         "9":"495",
         "10":"519",
         "11":"502",
         "12":"509"}
}

我试图了解如何使用不同的教程来做到这一点,但我似乎无法继续前进。我只需要提取它JSONArray并将其转换为String数组并将这些值存储到变量中。现在,我能够做到这一点:

public JSONObject solardata() throws ClientProtocolException, IOException, JSONException {
    StringBuilder sb = new StringBuilder(URL);
    HttpGet get = new HttpGet(sb.toString());
    HttpResponse r = client.execute(get);
    int status = r.getStatusLine().getStatusCode();
    if (status == 200) {
        HttpEntity e = r.getEntity();

这之后我该怎么办?我怎样才能取到JSONArray

4

4 回答 4

1
  1. Google is your friend. Please search before you ask. A simple search for "android httpget json" will give you tons of examples. Here's the first hit that does exactly what you want.
  2. There is not Array in your JSON. It is merely a normal JSON object with keys and values.
于 2013-09-13T17:14:49.830 回答
1

Well, a JSON array should look like this.-

{
    results:[
        elem1,
        elem2,
        ...
    ]
}

So first off, you have to make sure you're getting a JSON in the form you're expecting. Once you have a String with a valid JSON array, you can retrieve it with something like this.-

JSONObject jsonObj = new JSONObject(jsonString);
JSONArray jsonArray = jsonObj.getJSONArray("results");

for(int i = 0; i < jsonArray.length(); i ++) {
    JSONObject obj = jsonArray.getJSONObject(i);
    // Do whatever you need with obj
}

You can find a bunch of good tutorials, take this one as an example.

于 2013-09-13T17:15:28.150 回答
1

你可以这样做:

   try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpGet httpget = new HttpGet(url);
                HttpResponse response = httpclient.execute(httpget);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                statusCode = response.getStatusLine().getStatusCode();

            } catch (Exception e) {
                statusCode = -1;
                Log.e("log_tag", "Erro http " + e.toString());
            }
if (statusCode == 200) {

            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
            } catch (Exception e) {
                Log.e("log_tag", "Erro conversão " + e.toString());
            }

            try {
            JSONObject json = new JSONObject(result);
            JSONArray monthsJSON = json.getJSONArray("Months");
            ArrayList<HashMap<String, String>> monthsList = new ArrayList<HashMap<String,String>>();
            for (int i = 0; i < monthsJSON.length(); i++) {
                JSONObject object = (JSONObject) monthsJSON.get(i);
                HashMap<String, String> data = new HashMap<String, String>();
                String index = String.valueOf(i+1);
                data.put(index, object.getString(index));
                monthsList.add(data);
            }

        } catch (JSONException e) {
                Log.e("log_tag", "Erro dados " + e.toString());
            }
        }
于 2013-09-13T17:39:30.900 回答
0

这是我的简单 JSON。

yourJson :
[
 {"id":"1","name":"Jhohannes H Purba","address":"Kabanjahe"},
 {"id":"2","name":"Berkat Junaidi Banurea","address":"Sidikalang"},
 {"id":"3","name":"Totok BluesMan Silalahi","address":"Medan"}
]

对于将字段插入数组,我使用这个

            JSONArray arrayJson = new JSONArray(yourJson);

            for (int i = 0; i < arrayLagu.length(); i++) {    
                JSONObject jsonChildNode = arrayLagu.getJSONObject(i);
                int id = jsonChildNode.optInt("id");
                String name = jsonChildNode.optString("name");
                String address = jsonChildNode.optString("address");

                arrayId.add(i, id);
                arrayJudul.add(i, name);
                arrayLirik.add(i, address);
            }
于 2013-09-13T18:33:31.760 回答