-1

我是 Android 的初学者,我正在尝试从这种 JSON 格式中获取信息:

{
    "items": [
        {
            "id": "32",
            "monaie": "USD",
            "date": "2013-08-15",
            "achat": " 8.45766",
            "vente": "8.57466"
        },
        {
            "id": "33",
            "monaie": "CAD",
            "date": "2013-08-15",
            "achat": " 8.19735",
            "vente": "8.29833"
        },
        {
            "id": "34",
            "monaie": "EUR",
            "date": "2013-08-15",
            "achat": " 11.0547",
            "vente": "11.19249"
        },
        {
            "id": "35",
            "monaie": "JPY",
            "date": "2013-08-15",
            "achat": " 8.43443",
            "vente": "8.54948"
        },
        {
            "id": "36",
            "monaie": "GBP",
            "date": "2013-08-15",
            "achat": " 12.9400",
            "vente": "13.10321"
        },
        {
            "id": "37",
            "monaie": "USD",
            "date": "2013-08-15",
            "achat": " 8.45766",
            "vente": "8.57466"
        },
        {
            "id": "38",
            "monaie": "CAD",
            "date": "2013-08-15",
            "achat": " 8.19735",
            "vente": "8.29833"
        },
        {
            "id": "39",
            "monaie": "EUR",
            "date": "2013-08-15",
            "achat": " 11.0547",
            "vente": "11.19249"
        },
        {
            "id": "40",
            "monaie": "JPY",
            "date": "2013-08-15",
            "achat": " 8.43443",
            "vente": "8.54948"
        },
        {
            "id": "41",
            "monaie": "GBP",
            "date": "2013-08-15",
            "achat": " 12.9400",
            "vente": "13.10321"
        }
    ]
}

这是我使用的,但它不起作用:

  public static Devise getDevise(String data) throws JSONException {
    Devise devise = new Devise();
    JSONObject jObj = new JSONObject(data);
    JSONArray jArr = jObj.getJSONArray(0);
    JSONObject itemobj = jArr.getJSONObject(0);
    devise.nom = (getString("monaie", itemobj));
    devise.achat = (getString("achat", itemobj));
    devise.vente = (getString("vente", itemobj)); 
    return devise;  
}
4

3 回答 3

1

You must write the name of the array for defining first. I wrote how you are going to get the objects and arrays data from the JSON you gave.

    JSONObject jo = new JSONObject(data);
    JSONArray rootArray= jo.getJSONArray("items");
    int rootArrayLength=rootArray.length();
    for(int i=0;i<rootArrayLength;i++){
       int id = rootArray.getJSONObject(i).getInt("id");
       String mon = rootArray.getJSONObject(i).getString("monaie");
       // do same for other variables as well according to their type and your intention.
    }
于 2013-08-15T11:25:38.330 回答
1

如果你想要第一个对象

 JSONObject jobject = new JSONObject(data);
 JSONArray DeviseArray= jo.getJSONArray("items");
 JSONObject firstobject = DeviseArray.getJSONObject(0);
 Devise devise = new Devise();
 devise.nom = firstobject.getString("monaie");
 devise.achat = firstobject.getString("achat");
 devise.vente = firstobject.getString("vente");  
于 2013-08-15T12:33:45.607 回答
1

{表示json对象节点

[表示json数组节点

你的 json

{              // json object
    "items": [      // json array. array of items
        {           // json object  
            "id": "32",     
            "monaie": "USD",
            "date": "2013-08-15",
            "achat": " 8.45766",
            "vente": "8.57466"
        },

解析

JSONObject jObj;
    try {
        jObj = new JSONObject(data);
         JSONArray jArr = jObj.getJSONArray("items");
        for(int i=0;i<jArr.length();i++)
        {
           JSONObject itemobj = jArr.getJSONObject(i);
           String item = itemobj.getString("id");
           String monaie = itemobj.getString("monaie");
           String date = itemobj.getString("date"); 
           String achat = itemobj.getString("achat");
           String vente =itemobj.getString("vente");
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
于 2013-08-15T11:27:02.353 回答