json解析,中括号[array里没有object如何取数据
json解析,中括号[array里没有object怎么取数据
{
"others": [
{
"areacode": "000",
"citys": [],
"durl": "1565464qwe.com",
"jianpin": "qwe",
"name": "全国概要图",
"pinyin": "qwe",
"provincecode": "123123",
"size": "123123",
"version": "123123"
}
],
"provinces": [
{
"citys": [
{
"areacode": "123123",
"citycode": "123",
"durl": "456789789.com",
"jianpin": "hf",
"name": "合肥",
"pinyin": "hefei",
"size": "123123123",
"version": "123123123"
},
{
"areacode": "123123",
"citycode": "123123",
"durl": "789789.com",
"jianpin": "wh",
"name": "芜湖",
"pinyin": "wuhu",
"size": "123123",
"version": "123"
},
{
"areacode": "123",
"citycode": "340800",
"durl": "123123.com",
"jianpin": "aq",
"name": "安庆",
"pinyin": "anqing",
"size": "123",
"version": "123"
}
],
"durl": "http:/456456.com",
"jianpin": "ahs",
"name": "安徽省"
}
]
}
求大神帮下 json解析 怎么得到provinces里citys里某个城市的name
我现在解析到provinces 直接就出city名字了 但是得不到provinces 里省的名字 一开始就直接得到城市了名字
看下我的代码 求解
private void json() {
try {
//将json文件读取到buffer数组中
InputStream is = this.getResources().openRawResource(R.raw.map_data_v3);
byte[] buffer = new byte[is.available()];
is.read(buffer);
//将字节数组转换为以utf-8编码的字符串
String json = new String(buffer, "utf-8");
//将字符串json转换为json对象,以便于取出数据
JSONObject jsonObject = new JSONObject(json);
//解析provinces数组,解析中括号括起来的内容就表示一个数组,使用JSONArray对象解析
JSONArray others = jsonObject.getJSONArray("others");
JSONArray provinces = jsonObject.getJSONArray("provinces");
//StringBuffer操作字符串的一个高效类,保存解析的结果,以便于在TextView中显示
StringBuffer strBuf = new StringBuffer();
for(int i = 0; i < provinces.length(); i++) {
//取出数组的第一项
JSONObject item = provinces.getJSONObject(i);
//获得名称为provinces项的值
String durl = item.getString("durl");
String jianpin = item.getString("jianpin");
String name = item.getString("name");
String pinyin = item.getString("pinyin");
int provincecode = item.getInt("provincecode");
int size = item.getInt("size");
int version = item.getInt("version");
String areacode = item.getString("areacode");
JSONObject citys = item.getJSONObject("citys");
String citysname = citys.getString("name");
}
t1.setText(strBuf);
} catch (Exception e) {
t1.setText(e.getMessage());
}
------解决思路----------------------
怎么得到provinces里citys里某个城市的name
看到这句问题 我就醉了 由于不知道你到底问什么就看了下你的代码发现一个错误
JSONObject citys = item.getJSONObject("citys");
String citysname = citys.getString("name");
这里citys是个数组 所以
JSONArray citys= jsonObject.getJSONArray("citys")
然后fo循环吧
解析json你完全可以用gson 之类的框架 ,要自己解析那就认真的吧括号一个一个匹配
------解决思路----------------------
楼上正解,[]类型的是JSONArray {}类型的是JSONObject,转换时需要注意这些。
{"calendar":
{"calendarlist":
[
{"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false},
{"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false}
]
}
}
JSON转换
JSONObject jsonObject = new JSONObject(builder.toString())
.getJSONObject("calendar");
JSONArray jsonArray = jsonObject.getJSONArray("calendarlist");
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i);
CalendarInfo calendarInfo = new CalendarInfo();
calendarInfo.setCalendar_id(jsonObject2.getString("calendar_id"));
calendarInfo.setTitle(jsonObject2.getString("title"));
calendarInfo.setCategory_name(jsonObject2.getString("category_name"));
calendarInfo.setShowtime(jsonObject2.getString("showtime"));
calendarInfo.setEndtime(jsonObject2.getString("endshowtime"));
calendarInfo.setAllDay(jsonObject2.getBoolean("allDay"));
calendarInfos.add(calendarInfo);
}
{
"others": [
{
"areacode": "000",
"citys": [],
"durl": "1565464qwe.com",
"jianpin": "qwe",
"name": "全国概要图",
"pinyin": "qwe",
"provincecode": "123123",
"size": "123123",
"version": "123123"
}
],
"provinces": [
{
"citys": [
{
"areacode": "123123",
"citycode": "123",
"durl": "456789789.com",
"jianpin": "hf",
"name": "合肥",
"pinyin": "hefei",
"size": "123123123",
"version": "123123123"
},
{
"areacode": "123123",
"citycode": "123123",
"durl": "789789.com",
"jianpin": "wh",
"name": "芜湖",
"pinyin": "wuhu",
"size": "123123",
"version": "123"
},
{
"areacode": "123",
"citycode": "340800",
"durl": "123123.com",
"jianpin": "aq",
"name": "安庆",
"pinyin": "anqing",
"size": "123",
"version": "123"
}
],
"durl": "http:/456456.com",
"jianpin": "ahs",
"name": "安徽省"
}
]
}
求大神帮下 json解析 怎么得到provinces里citys里某个城市的name
我现在解析到provinces 直接就出city名字了 但是得不到provinces 里省的名字 一开始就直接得到城市了名字
看下我的代码 求解
private void json() {
try {
//将json文件读取到buffer数组中
InputStream is = this.getResources().openRawResource(R.raw.map_data_v3);
byte[] buffer = new byte[is.available()];
is.read(buffer);
//将字节数组转换为以utf-8编码的字符串
String json = new String(buffer, "utf-8");
//将字符串json转换为json对象,以便于取出数据
JSONObject jsonObject = new JSONObject(json);
//解析provinces数组,解析中括号括起来的内容就表示一个数组,使用JSONArray对象解析
JSONArray others = jsonObject.getJSONArray("others");
JSONArray provinces = jsonObject.getJSONArray("provinces");
//StringBuffer操作字符串的一个高效类,保存解析的结果,以便于在TextView中显示
StringBuffer strBuf = new StringBuffer();
for(int i = 0; i < provinces.length(); i++) {
//取出数组的第一项
JSONObject item = provinces.getJSONObject(i);
//获得名称为provinces项的值
String durl = item.getString("durl");
String jianpin = item.getString("jianpin");
String name = item.getString("name");
String pinyin = item.getString("pinyin");
int provincecode = item.getInt("provincecode");
int size = item.getInt("size");
int version = item.getInt("version");
String areacode = item.getString("areacode");
JSONObject citys = item.getJSONObject("citys");
String citysname = citys.getString("name");
}
t1.setText(strBuf);
} catch (Exception e) {
t1.setText(e.getMessage());
}
------解决思路----------------------
怎么得到provinces里citys里某个城市的name
看到这句问题 我就醉了 由于不知道你到底问什么就看了下你的代码发现一个错误
JSONObject citys = item.getJSONObject("citys");
String citysname = citys.getString("name");
这里citys是个数组 所以
JSONArray citys= jsonObject.getJSONArray("citys")
然后fo循环吧
解析json你完全可以用gson 之类的框架 ,要自己解析那就认真的吧括号一个一个匹配
------解决思路----------------------
楼上正解,[]类型的是JSONArray {}类型的是JSONObject,转换时需要注意这些。
{"calendar":
{"calendarlist":
[
{"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false},
{"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false}
]
}
}
JSON转换
JSONObject jsonObject = new JSONObject(builder.toString())
.getJSONObject("calendar");
JSONArray jsonArray = jsonObject.getJSONArray("calendarlist");
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i);
CalendarInfo calendarInfo = new CalendarInfo();
calendarInfo.setCalendar_id(jsonObject2.getString("calendar_id"));
calendarInfo.setTitle(jsonObject2.getString("title"));
calendarInfo.setCategory_name(jsonObject2.getString("category_name"));
calendarInfo.setShowtime(jsonObject2.getString("showtime"));
calendarInfo.setEndtime(jsonObject2.getString("endshowtime"));
calendarInfo.setAllDay(jsonObject2.getBoolean("allDay"));
calendarInfos.add(calendarInfo);
}