我无法解析来自Response的数据:
这是我的片段:
Response = '{"sys":"[{\"division\":\"Barisal\",\"district\":\"Pirojpur Zila\",\"upazilla\":\"Mathbaria Upazila\"},{\"division\":\"Barisal\",\"district\":\"Jhalokati Zila\",\"upazilla\":\"Rajapur Upazila\"},{\"division\":\"Barisal\",\"district\":\"Barguna Zila\",\"upazilla\":\"Amtali Upazila\"},{\"division\":\"Barisal\",\"district\":\"Barisal Zila\",\"upazilla\":\"Banari Para Upazila\"},{\"division\":\"Barisal\",\"district\":\"Pirojpur Zila\",\"upazilla\":\"Pirojpur Sadar Upazila\"},{\"division\":\"Barisal\",\"district\":\"Barisal Zila\",\"upazilla\":\"Muladi Upazila\"}]"}';
JSONObject json = new JSONObject(response);
JSONArray userdetails = json.getJSONArray("sys");
for (int i=0; i<userdetails.length(); i++) {
JSONObject user = userdetails.getJSONObject(i);
String division = user.getString("division");
String district = user.getString("district");
String upazilla = user.getString("upazilla");
}
我调试代码。尝试检查用户详细信息长度时代码停止。
有什么想法吗?
答案 0 :(得分:1)
应该没有&#34;在[{nor after}]前面
这应该有效:
String response = "{\"sys\":[{\"division\":\"Barisal\",\"district\":\"Pirojpur Zila\",\"upazilla\":\"Mathbaria Upazila\"},{\"division\":\"Barisal\",\"district\":\"Jhalokati Zila\",\"upazilla\":\"Rajapur Upazila\"},{\"division\":\"Barisal\",\"district\":\"Barguna Zila\",\"upazilla\":\"Amtali Upazila\"},{\"division\":\"Barisal\",\"district\":\"Barisal Zila\",\"upazilla\":\"Banari Para Upazila\"},{\"division\":\"Barisal\",\"district\":\"Pirojpur Zila\",\"upazilla\":\"Pirojpur Sadar Upazila\"},{\"division\":\"Barisal\",\"district\":\"Barisal Zila\",\"upazilla\":\"Muladi Upazila\"}]}";
JSONObject json = new JSONObject(response);
JSONArray userdetails = json.getJSONArray("sys");
for (int i=0; i<userdetails.length(); i++)
{
JSONObject user = userdetails.getJSONObject(i);
String division = user.getString("division");
String district = user.getString("district");
String upazilla = user.getString("upazilla");
}
答案 1 :(得分:0)
请尝试以下格式。即:
String response = "{\"sys\":[{\"division\":\"Barisal\",\"district\":\"Pirojpur Zila\",\"upazilla\":\"Mathbaria Upazila\"},{\"division\":\"Barisal\",\"district\":\"Jhalokati Zila\",\"upazilla\":\"Rajapur Upazila\"},{\"division\":\"Barisal\",\"district\":\"Barguna Zila\",\"upazilla\":\"Amtali Upazila\"},{\"division\":\"Barisal\",\"district\":\"Barisal Zila\",\"upazilla\":\"Banari Para Upazila\"},{\"division\":\"Barisal\",\"district\":\"Pirojpur Zila\",\"upazilla\":\"Pirojpur Sadar Upazila\"},{\"division\":\"Barisal\",\"district\":\"Barisal Zila\",\"upazilla\":\"Muladi Upazila\"}]}";
在没有“\”的情况下测试JSON format here &安培;您的json解析代码是正确的
答案 2 :(得分:0)
你的json对象是:
{
"sys": [{
"division": "Barisal",
"district": "Pirojpur Zila",
"upazilla": "Mathbaria Upazila"
}, {
"division": "Barisal",
"district": "Jhalokati Zila",
"upazilla": "Rajapur Upazila"
}, {
"division": "Barisal",
"district": "Barguna Zila",
"upazilla": "Amtali Upazila"
}, {
"division": "Barisal",
"district": "Barisal Zila",
"upazilla": "Banari Para Upazila"
}, {
"division": "Barisal",
"district": "Pirojpur Zila",
"upazilla": "Pirojpur Sadar Upazila"
}, {
"division": "Barisal",
"district": "Barisal Zila",
"upazilla": "Muladi Upazila"
}]
}
对于自动序列化和反序列化,请使用Gson库。这可以在Gson中以非常简单的方式完成。转到jsonschematopojo.org并将您的json转换为pojo类。 json对象的结果pojo类是:
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class YourJsonClass {
@SerializedName("sys")
@Expose
private List<Sy> sys = null;
public List<Sy> getSys() {
return sys;
}
public void setSys(List<Sy> sys) {
this.sys = sys;
}
public class Sy {
@SerializedName("division")
@Expose
private String division;
@SerializedName("district")
@Expose
private String district;
@SerializedName("upazilla")
@Expose
private String upazilla;
public String getDivision() {
return division;
}
public void setDivision(String division) {
this.division = division;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public String getUpazilla() {
return upazilla;
}
public void setUpazilla(String upazilla) {
this.upazilla = upazilla;
}
}
现在要访问它,现在变得更简单了,
String response = "{\"sys\":[{\"division\":\"Barisal\",\"district\":\"Pirojpur Zila\",\"upazilla\":\"Mathbaria Upazila\"},{\"division\":\"Barisal\",\"district\":\"Jhalokati Zila\",\"upazilla\":\"Rajapur Upazila\"},{\"division\":\"Barisal\",\"district\":\"Barguna Zila\",\"upazilla\":\"Amtali Upazila\"},{\"division\":\"Barisal\",\"district\":\"Barisal Zila\",\"upazilla\":\"Banari Para Upazila\"},{\"division\":\"Barisal\",\"district\":\"Pirojpur Zila\",\"upazilla\":\"Pirojpur Sadar Upazila\"},{\"division\":\"Barisal\",\"district\":\"Barisal Zila\",\"upazilla\":\"Muladi Upazila\"}]}";
Gson g1 = new Gson();
YourJsonClass response2 = g1.fromJson(response, YourJsonClass.class);
现在,您需要根据自己的方便迭代response2
对象。
for(int i=0; i < response2.getSys().size(); i++) {
System.out.println(response2.getSys().get(i).getDivision());
System.out.println(response2.getSys().get(i).getDistrict());
System.out.println(response2.getSys().get(i).getUpazilla());
}
答案 3 :(得分:0)
示例代码:
String JSON_DATA =
"{"
+ " \"geodata\": ["
+ " {"
+ " \"id\": \"1\","
+ " \"name\": \"Julie Sherman\","
+ " \"gender\" : \"female\","
+ " \"latitude\" : \"37.33774833333334\","
+ " \"longitude\" : \"-121.88670166666667\""
+ " },"
+ " {"
+ " \"id\": \"2\","
+ " \"name\": \"Johnny Depp\","
+ " \"gender\" : \"male\","
+ " \"latitude\" : \"37.336453\","
+ " \"longitude\" : \"-121.884985\""
+ " }"
+ " ]"
+ "}";
JSONObject json;
JSONArray geodetails = null;
JSONObject user;
try {
json = new JSONObject(JSON_DATA);
geodetails = json.getJSONArray("geodata");
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < geodetails.length(); i++) {
try {
user = geodetails.getJSONObject(i);
String name = user.getString("name");
String gender = user.getString("gender");
String latitude = user.getString("latitude");
Log.d("Json response", " " + name+" "+gender+" "+latitude);
} catch (JSONException e) {
e.printStackTrace();
}
}
答案 4 :(得分:0)
您的Json回复不正确。在JsonArray之前不应该有任何代码,响应应该像这样
{"sys":[{"division":"Barisal","district":"Pirojpur Zila","upazilla":"Mathbaria Upazila"},{"division":"Barisal","district":"Jhalokati Zila","upazilla":"Rajapur Upazila"},{"division":"Barisal","district":"Barguna Zila","upazilla":"Amtali Upazila"},{"division":"Barisal","district":"Barisal Zila","upazilla":"Banari Para Upazila"},{"division":"Barisal","district":"Pirojpur Zila","upazilla":"Pirojpur Sadar Upazila"},{"division":"Barisal","district":"Barisal Zila","upazilla":"Muladi Upazila"}]}