我在我的android
中打印这个json数组它包含一个数组内的数组
什么是android代码?
这是我的php输出
[{"result":"true","user":[{"id":"26","name":"Manali Ashar","email":"123","password":"manali.ashar19@gmail.com","address":"chital road","city":"amreli","contact":"9402312345","qualification":"M.D","experience":"2","department":"Pediatric","age":"22","from_time":"05:15:00","to_time":"07:15:00","status":"1","current_date_time":"2016-04-02 07:18:26"}]}]
答案 0 :(得分:2)
尝试删除所有数组括号[ ]
,因为您只需要JSON对象而不是json对象数组。然后数据看起来像
{"result":"true","user":{"id":"26","name":"Manali Ashar","email":"123","password":"manali.ashar19@gmail.com","address":"chital road","city":"amreli","contact":"9402312345","qualification":"M.D","experience":"2","department":"Pediatric","age":"22","from_time":"05:15:00","to_time":"07:15:00","status":"1","current_date_time":"2016-04-02 07:18:26"}}
现在的android代码:
try {
JSONObject json = new JSONObject(result);
JSONObject user= json.getJSONObject("user");
String name= user.getString("name");
} catch (JSONException e) {
e.printStackTrace();
}
答案 1 :(得分:2)
try {
//send response
JSONArray jsonArray = JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
if(!jsonObject.isNull("result")){
String result = jsonObject.getString("result");
if(result.equals("true")){
JSONArray userArray = jsonObject.getJSONArray("user");
for(int j=0; j<userArray.length(); j++){
jsonObject = userArray.getJSONObject(j);
String id = isJsonValid(jsonObject, "id");
String name = isJsonValid(jsonObject, "name");
String email = isJsonValid(jsonObject, "email");
String password = isJsonValid(jsonObject, "password");
String address = isJsonValid(jsonObject, "address");
String city = isJsonValid(jsonObject, "city");
String contact = isJsonValid(jsonObject, "contact");
String qualification = isJsonValid(jsonObject, "qualification");
String experience = isJsonValid(jsonObject, "experience");
String department = isJsonValid(jsonObject, "department");
String to_time = isJsonValid(jsonObject, "to_time");
String age = isJsonValid(jsonObject, "age");
String from_time = isJsonValid(jsonObject, "from_time");
String status = isJsonValid(jsonObject, "status");
String current_date_time = isJsonValid(jsonObject, "current_date_time");
}
}else{
//do something
}
}
}
}catch (JSONException je){
je.printStackTrace();
}
public String isJsonValid(JSONObject jsonObject, String key) throws JSONException {
//check key is present or not
if(!jsonObject.isNull(key)){
return jsonObject.getString(key);
}else{
return "N/A";
}
}
答案 2 :(得分:2)
String data="[{"result":"true","user":[{"id":"26","name":"Manali Ashar","email":"123","password":"manali.ashar19@gmail.com","address":"chital road","city":"amreli","contact":"9402312345","qualification":"M.D","experience":"2","department":"Pediatric","age":"22","from_time":"05:15:00","to_time":"07:15:00","status":"1","current_date_time":"2016-04-02 07:18:26"}]}]";
try
{
JSONArray array=new JSONArray(data);
JSONObject jsonObject=array.getJSONObject(0);
String returnword=jsonObject.getString("return");
JSONArray users=jsonObject.getJSONArray("user");
for(int i=0;i<users.length();i++)
{
JSONObject object=users.getJSONObject(i);
String id=object.getString("id");
String name=object.getString("name");
String email=object.getString("email");
String password=object.getString("password");
String address=object.getString("address");
String city=object.getString("city");
String contact=object.getString("contact");
String qualification=object.getString("qualification");
String experience=object.getString("experience");
String department=object.getString("department");
String age=object.getString("age");
String from_time=object.getString("from_time");
String to_time=object.getString("to_time");
String status=object.getString("status");
String current_date_time=object.getString("current_date_time");
}
}
catch (JSONException e)
{
e.printStackTrace();
}
这是解析任何类型json的简单方法。
答案 3 :(得分:1)
您可以使用Gson库
第1步:为json数据创建POJO
public class Data {
public String result;
public List<User> user;
}
public class User {
public String status;
public String department;
public String password;
public String contact;
public String city;
public String id;
public String qualification;
public String email;
public String address;
public String from_time;
public String name;
public String age;
public String current_date_time;
public String experience;
public String to_time;
}
第2步:使用Gson库解析JSON
final Gson gson = new Gson();
由于它是一组数据,我们需要从json字符串中获取 List
List<Data> response = gson.fromJson(JSON_DATA, new TypeToken<List<Data>>() {}.getType());
第3步:获取值
Data data = response.get(0);
User user = data.user.get(0);
同样打印结果
Log.d("JsonExample","Name: " + user.name);
...
...