我对android开发真的很新....我有这个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"
}
]
]
这是我的方法:
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;
}
这是我的解析器:
// 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;
}
}
我必须在字符串数组中保存所有信息(从“IDalunno”和“Username”到“TipoVoto”和“Voto”),但我真的不知道......每个提示都可能是宝贵的。
编辑:我发现,我必须在我的方法getJSONFromUrl中返回一个JSONArray,然后使用obj.getJSONObject(0).getString(“IDalunno”)来访问该数组
答案 0 :(得分:0)
答案 1 :(得分:0)
例如,您将json对象存储在变量 数据
中var data = {
"number": "001",
"name": "Jill",
"date": "2014. January 01."
}
{
"number": "002",
"name": "John",
"date": "2014. March 03."
}
您可以检索这样的每个元素:
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.
一旦你知道如何检索每个元素,就可以按自己喜欢的方式保存(使用循环或其他)
答案 2 :(得分: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;
});