如何json解析这些数据?
我没有解析它。
{"boh" : {
"-LBktL2BIRnG6yZPzX9M" : {
"address" : "Улаанбаатар",
"firstname" : "Болд",
"floatt" : "9",
"lastname" : "Бат ",
"title" : "улсын начин"
},
"-LBktTUZMDLqkoMR49Gv" : {
"address" : "Улаанбаатар",
"firstname" : "Эрдэнэ",
"floatt" : "",
"lastname" : "Болд",
"title" : "залуу бөх"
},
"-LBkt_OIvRGD-r_L40EH" : {
"address" : "Улаанбаатар",
"firstname" : "Нямхүү",
"floatt" : "5",
"lastname" : "Буянжаргал",
"title" : "улсын заан"
}
};
我写了下面的代码:;
JSONObject obj1 = new JSONObject(jsonString);
JSONArray result = obj1.getJSONArray("boh");
for(int i=0;i<=result.length();i++)
{
JSONObject c = result.getJSONObject(i);
String address=c.getString("address");
String firstname=c.getString("firstname");
String floatt=c.getString("floatt");
String lastname=c.getString("lastname");
String title=c.getString("title");
};
但是,JSONException错误
如何在android中解析解析帮助我?
答案 0 :(得分:1)
使用Iterator可以传递数据
JSONObject obj1 = new JSONObject(jsonString);
JSONObject result = obj1.getJSONObject("boh");
Iterator<String> iter = result.keys();
while (iter.hasNext()) {
String key = iter.next();
JSONObject c = result.getJSONObject(key);
String address=c.getString("address");
String firstname=c.getString("firstname");
String floatt=c.getString("floatt");
String lastname=c.getString("lastname");
String title=c.getString("title");
}
答案 1 :(得分:0)
使用Gson库
内部地图值类
public class Example{
@SerializedName("firstname")
@Expose
private String firstname;
@SerializedName("address")
@Expose
private String address;
@SerializedName("floatt")
@Expose
private Integer floatt;
@SerializedName("title")
@Expose
private String title;
@SerializedName("lastname")
@Expose
private String lastname;
public void setFirstname(String firstname){
this.firstname=firstname;
}
public String getFirstname(){
return firstname;
}
public void setAddress(String address){
this.address=address;
}
public String getAddress(){
return address;
}
public void setFloatt(Integer floatt){
this.floatt=floatt;
}
public Integer getFloatt(){
return floatt;
}
public void setTitle(String title){
this.title=title;
}
public String getTitle(){
return title;
}
public void setLastname(String lastname){
this.lastname=lastname;
}
public String getLastname(){
return lastname;
}
}
用于解析
Map<String, HashMap<String, Example>> categoryMap = new Gson().fromJson(br, new TypeToken<Map<String, HashMap<String, Example>>>(){}.getType());
获取价值
Iterator it = categoryMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
Iterator it = ((HashMap<String, Example>>)categoryMap.get(pair.getKey())).entrySet().iterator();
while (it.hasNext()) {
System.out.println(pair.getKey() + " = " + pair.getValue());
Example mExample = (Example)categoryMap.get(pair.getKey());//
}
it.remove(); // avoids a ConcurrentModificationException
}
答案 2 :(得分:0)
boh
不是JSON数组,它是带子项的JSON对象。如果boh
是一个数组,它将如下所示(注意方括号[ ]
):
{"boh" : [
{
"address" : "Улаанбаатар",
"firstname" : "Болд",
"floatt" : "9",
"lastname" : "Бат ",
"title" : "улсын начин"
},
{
"address" : "Улаанбаатар",
"firstname" : "Эрдэнэ",
"floatt" : "",
"lastname" : "Болд",
"title" : "залуу бөх"
},
{
"address" : "Улаанбаатар",
"firstname" : "Нямхүү",
"floatt" : "5",
"lastname" : "Буянжаргал",
"title" : "улсын заан"
}]
}
因此,为了访问这些子对象,您需要将boh
视为JSONObject
并按顺序访问其子代:
JSONObject obj1 = new JSONObject(jsonString);
JSONObject result = obj1.getJSONObject("boh");
Iterator<String> keys = result.keys();
while(keys.hasNext()) {
String key = keys.next();
JSONObject c = result.getJSONObject(key);
String address=c.getString("address");
String firstname=c.getString("firstname");
String floatt=c.getString("floatt");
String lastname=c.getString("lastname");
String title=c.getString("title");
};