0

I'm really new about android development.... I have this JSONObject:

[
{
"IDalunno":"1",
"Username":"federicove",
"Password":"5ecb232b535685f95fa7bc19e436c79e",
"Nome":"Federico",
"Cognome":"Vergallo",
"Classe":"5",
"Sezione":"A",
"Indirizzo":"Informatica Abacus"
},
{
"0":{
"DataAssenza":"2013-09-26",
"TipoAssenza":"Ritardo"
},
"2":{
"DataAssenza":"2013-12-11",
"TipoAssenza":"Assenza"
},
"4":{
"DataAssenza":"2013-12-16",
"TipoAssenza":"Assenza"
}
},
[
{
"Materia":"Italiano",
"DataVoto":"2014-04-01",
"TipoVoto":"Scritto",
"Voto":"8"
},
{
"Materia":"Italiano",
"DataVoto":"2014-04-02",
"TipoVoto":"Orale",
"Voto":"7"
},
{
"Materia":"Storia",
"DataVoto":"2014-04-09",
"TipoVoto":"Orale",
"Voto":"7"
},
{
"Materia":"Inglese",
"DataVoto":"2014-03-04",
"TipoVoto":"Orale",
"Voto":"9"
},
{
"Materia":"Inglese",
"DataVoto":"2014-03-10",
"TipoVoto":"Orale",
"Voto":"8"
},
{
"Materia":"Matematica",
"DataVoto":"2014-02-03",
"TipoVoto":"Scritto",
"Voto":"8"
}
]
]

This is my method:

  protected String[] doInBackground(String...params) {
    String url = "http://federicovergallo.altervista.org/blog/prova.php";
    JSONParser sitoLogin = new JSONParser();
    try{
        JSONObject obj = sitoLogin.getJSONFromUrl(url, params);

        String[] risultatoLogin = new String [3];

        risultatoLogin[0] = obj.getString("IDalunno");
        risultatoLogin[1] = obj.getString("Nome");
        risultatoLogin[2] = obj.getString("Cognome");

        return risultatoLogin;
        //this doesn't work, it returns null everytime
    }
    catch (Exception e){

    }
    return null;
}

This is my parser:

// costruttore
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url, String[] parametri) {

    // Preparo i dati da passare tramite POST
    ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("username", parametri[0]));
    params.add(new BasicNameValuePair("password", parametri[1]));
    // HTTP request
    Log.i("User", parametri[0]);
    Log.i("Password", parametri[1]);
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        httppost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httppost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        is.close();
        json = sb.toString();
        Log.e("JSON", json);
    } catch (Exception e) {
        Log.e("Buffer Error", "Errore nella conversione " + e.toString());
    }

    //trasformo la stringa in un oggetto JSON
    //se abbiamo fatto errori lato server, qui vengono fuori ;)
    try {
        jObj = new JSONObject(json);            
    } catch (JSONException e) {
        Log.e("JSON Parser", "Errore nel parse " + e.toString());
    }

    // return l'oggetto JSONObject
    return jObj;

}

}

I have to save all information (from "IDalunno" and "Username" to "TipoVoto" and "Voto") in a String Array but I really don't know how ... Every hint could be precious.

EDIT: i've found out, I had to return a JSONArray in my method getJSONFromUrl and then access to the array using obj.getJSONObject(0).getString("IDalunno")

  • [Json parser tutorial](http://www.tutorialspoint.com/android/android_json_parser.htm) can help or [can use gson](http://stackoverflow.com/questions/21480634/unable-to-loop-through-dynamic-json-string-recursively-in-android/21480997#21480997) – Pararth Jun 10 '14 at 10:30
  • Post you parsing code. – Hariharan Jun 10 '14 at 10:45

3 Answers3

0

Take a look at the documentation and, first of all, try anything :)

http://developer.android.com/reference/org/json/package-summary.html

Didac Perez Parera
  • 3,734
  • 3
  • 52
  • 87
  • This is a strange JSONObject because it's not a JSONArray and using instructions like obj.getString("IDalunno") doesn't work... It would be all simpler if it was a JSONArray but I can't figure it out how to convert – federicove Jun 10 '14 at 10:39
0

For example you store your json object in variable data

var data = {
"number": "001",
"name": "Jill",
"date": "2014. January 01."
}

{
"number": "002",
"name": "John",
"date": "2014. March 03."
}

You can retrieve each element like this:

data[0].number // is 001
data[1].number // is 002

data[0].name  // is Jill
data[1].name  // is John

data[0].date // is 2014. January 01.
data[1].date // is 2014. March 03.

Once you know how to retrieve each element, you can save it the way you like (using loops or whatever)

Rai Ammad Khan
  • 1,521
  • 2
  • 14
  • 26
0
var data = [{
"number": "001",
"name": "Jill",
"date": "2014. January 01."
},
{
"number": "002",
"name": "John",
"date": "2014. March 03."
}];

var ArrayName = new Array(); 
var ArrayNumber = new Array();
var ArrayDate = new Array();  

int i=0;

data.forEach(function(v){

   ArrayName[i]=v.name;
   ArrayNumber[i]=v.number;
   ArrayDate[i]=v.date
   i=i+1;

});
Rai Ammad Khan
  • 1,521
  • 2
  • 14
  • 26