我有json数据类型
{"email":"mikekhlau@gmail.com","contact":[{"contact_name":"Mike Lau","contact_no":"019-3331976"},{"contact_name":"Jason Lau","contact_no":"013-2711188"},{"contact_name":"Margaret Lau","contact_no":"019-3122281"}]}
如何获取电子邮件,contact_name和contact_no?
答案 0 :(得分:1)
var json = {"email":"mikekhlau@gmail.com","contact":[{"contact_name":"Mike Lau","contact_no":"019-3331976"},{"contact_name":"Jason Lau","contact_no":"013-2711188"},{"contact_name":"Margaret Lau","contact_no":"019-3122281"}]}
您可以通过..
获取数据json.email
" mikekhlau@gmail.com"
json.contact[0].contact_name
" Mike Lau"
json.contact[0].contact_no
" 019-3331976"
答案 1 :(得分:1)
try {
JSONObject jsonRootObject = new JSONObject(strJson);
String email = jsonRootObject .getString("email");
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = jsonRootObject.optJSONArray("contacts");
//Iterate the jsonArray and print the info of JSONObjects
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String contact_name = jsonObject.getString("contact_name");
String contact_no= jsonObject.getString("contact_no");
}
} catch (JSONException e) {e.printStackTrace();}
}
use the above code