-1

I'm a beginner in Android, i'm trying to get the information from this JSON format :

{
    "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"
        }
    ]
}

This is what i used but it doesn't work :

  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;  
}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
deltascience
  • 3,321
  • 5
  • 42
  • 71
  • 2
    Please (always) state what "doesn't work" means. What do you see when you do it? Compile error? Runtime error? Exception? What is the unexpected result? What do you _mean_ when you say that it does not work? – Christian Neverdal Aug 15 '13 at 11:25
  • look through this http://stackoverflow.com/questions/3605077/how-can-i-parse-this-json-in-android and http://stackoverflow.com/questions/12152468/parse-this-json-in-android – surhidamatya Aug 15 '13 at 11:38
  • check this link too http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ i am sure you will get your answer. – surhidamatya Aug 15 '13 at 11:44
  • My suggestion is GSON or Jackson, they might help you if your data-struct will not be changed. – TeeTracker Aug 15 '13 at 12:35
  • did you try the below? does it work for you – Raghunandan Aug 15 '13 at 15:16

3 Answers3

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.
    }
canova
  • 3,965
  • 2
  • 22
  • 39
1

{ represents json object node

[ represents json array node

Your 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"
        },

To parse

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();
    }
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
1

If you want first object

 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");  
SHASHIDHAR MANCHUKONDA
  • 3,302
  • 2
  • 19
  • 40