请问 从网络数据库获取的数据如下:
rev={"userid":"8012be515cc9244ef263e91d2b442349","username":"123456789",“service":{"name":"普通会员","endtime":"2015-04-12 15:23:54","area":{"times":"2","num":"1","area":[""]}}}
想获取 service里边内容。
但是当按照 以下代码解析的时候,会出现以下提示:
02-15 13:32:56.582: D/HttpApi(22030): myinfo: rev={"userid":"8012be515cc9244ef263e91d2b442349","username":"123456789","service":{"name":"普通会员","endtime":"2015-04-12 15:23:54","area":{"times":"2","num":"1","area":[""]}} }
02-15 13:32:56.582: D/HttpApi(22030): Exception e org.json.JSONException: Value {"endtime":"2015-04-12 15:23:54","area":{"times":"2","num":"1","area":[""]},"name":"普通会员"} at service of type org.json.JSONObject cannot be converted to JSONArray
请问 以下的代码怎么修改才能解析出 service里边的数据呢? 麻烦会的朋友帮忙回复。谢啦
代码如下:
public List<DistributeFirst> bute_first ( String access_token ) {
。。。。。
if( code==200) {
String rev =EntityUtils.toString(response.getEntity()); //id
Log.d(TAG,"yong: rev=" +rev);
json= new String(rev);
return parsejsonbutefirst(json);
}
。。。。。。
}
private static List<MyInformation> parsejsonmyinfo(String string) throws Exception {
List<MyInformation> list = new ArrayList<MyInformation>();
JSONObject obj1 = new JSONObject(string);
String userid = obj1.getString("userid");
//Log.d(TAG,"yong++ parsejsonbroadcastfirst id ="+item.getString("id"));
String username = obj1.getString("username"); //username
JSONArray jsonArray = obj1.getJSONArray("service");
for( int i=0;i<jsonArray.length();i++ ) {
JSONObject item = jsonArray.getJSONObject(i);
name1 = item.getString("name");
endtime = item.getString("endtime");
JSONArray jsonArray1 = obj1.getJSONArray("area");
for( int j=0;j<jsonArray1.length();j++ ) {
JSONObject item1 = jsonArray.getJSONObject(j);
times = item.getString("times");
num = item.getString("num");
area2 = item.getString("area");
list.add(new MyInformation(userid,username,name1,endtime,times,num,area2));
}
}
return list;
}
------解决思路----------------------
你那个service是一个json对象,而不是一个json数组,换成json对象试下吧
------解决思路----------------------
JSONArray jsonArray = obj1.getJSONArray("service");
改为JSONObject object = obj1.getJSONObject("service");
------解决思路----------------------
你这个json怎么传递的?通过handler?
------解决思路----------------------
package com.wztech.mobile.common;
import java.io.IOException;
import java.util.HashMap;
import org.codehaus.jackson.map.ObjectMapper;
import com.google.gson.Gson;
/**
* Json Utils
* @param obj
* @return
*/
public class JsonUtil {
/**
* DESEncrypt Key
*/
private static final String KEY = "12345abcde";
/**
* Pojo To Json
* @param obj
* @return
*/
public static String pojoToJson(Object obj){
Gson gson = new Gson();
String gsonStr = gson.toJson(obj);
return gsonStr;
}
/**
* Pojo To Json
* @param obj
* @return
*/
public static String pojoToJsonEncode(Object obj){
Gson gson = new Gson();
String gsonStr = gson.toJson(obj);
String resJson = new String(Base64Util.encode(gsonStr.toString().getBytes()));
resJson = DESEncryptUtils.encode(KEY, resJson);
return resJson;
}
/**
* Json To String
* @param obj
* @return
*/
public static String jsonDecode(String str){
try {
return new String(Base64Util.decode(DESEncryptUtils.decode(KEY, str)));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Json To Map
* @param obj
* @throws IOException
* @throws JsonMappingException
* @throws JsonParseException
*/
public static HashMap<String, Object> jsonToMap(String json) throws IOException{
ObjectMapper mapper = new ObjectMapper();
HashMap<String, Object> map = mapper.readValue(json, HashMap.class);
return map;
}
/**
* Json to Bean
*/
public static Object pojoToMap(String json, Object javaBean) {
Gson gson = new Gson();
Object fromJson = gson.fromJson( json.toString() ,javaBean.getClass());
return fromJson;
}
}
------解决思路----------------------
方法1:
/**
* Json To Map
* @param obj
* @throws IOException
* @throws JsonMappingException
* @throws JsonParseException
*/
public static HashMap<String, Object> jsonToMap(String json) throws IOException{
ObjectMapper mapper = new ObjectMapper();
HashMap<String, Object> map = mapper.readValue(json, HashMap.class);
return map;
}
用这个,会把json数据,不管什么格式的都直接转化成map,你转化后把值打印出来,然后get就可以了。
ObjectMapper mapper = new ObjectMapper();
HashMap<String, Object> map = mapper.readValue(json, HashMap.class);
方法2:
/**
* Json to Bean
*/
public static Object pojoToMap(String json, Object javaBean) {
Gson gson = new Gson();
Object fromJson = gson.fromJson( json.toString() ,javaBean.getClass());
return fromJson;
}
用谷歌的也很方便,把数据格式用对应的POJO写好,把POJO传进去,json就会解析成对应的bean对象,直接从对象取相应的值就可以,不会的话,可以再百度学一下!
Gson gson = new Gson();
Object fromJson = gson.fromJson( json.toString() ,javaBean.getClass());
希望,帮到你!
------解决思路----------------------
{
"userid": "8012be515cc9244ef263e91d2b442349",
"username": "123456789",
"service": {
"name": "普通会员",
"endtime": "2015-04-12 15:23:54",
"area": {
"times": "2",
"num": "1",
"area": [
""
]
}
}
}
private static List<MyInformation> parsejsonmyinfo(String string) throws Exception {
List<MyInformation> list = new ArrayList<MyInformation>();
JSONObject obj1 = new JSONObject(string);
String userid = obj1.getString("userid");
String username = obj1.getString("username"); //username
JSONObject jsonobj = obj1.getJSONObject("service");
name1 = jsonobj .getString("name");
endtime = jsonobj .getString("endtime");
JSONObject jsonobj1 = jsonobj.getJSONObject("area");
times = jsonobj1.getString("times");
num = jsonobj1.getString("num");
//至于数组的话貌似没看明白,不太清楚
area2 = item.getString("area");
list.add(new MyInformation(userid,username,name1,endtime,times,num,area2));
return list;
}