解析JSON多线Android

时间:2013-08-15 11:19:27

标签: android json

我是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;  
}

3 个答案:

答案 0 :(得分:1)

您必须首先编写用于定义的数组名称。我写了你将如何从你给出的JSON中获取对象和数组数据。

    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.
    }

答案 1 :(得分: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();
    }

答案 2 :(得分: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");