上一篇已经介绍了万能解析Json的原理,但是全部代码都写在一个类里就失去了重复利用的作用,所以对代码做作了提取,单独写在一个类里。
具体讲解操作
Json数据转化成hashmap(JsonToHashMap.class)
public class JsonToHashMap {
String url;
Handler handler;
public JsonToHashMap(String url, Handler handler){
this.url=url;
this.handler=handler;//注释1
sendRequsetWithOKHttp(url);
}
public void sendRequsetWithOKHttp(String Url) {
new Thread(new Runnable() {
@Override
public void run() {
try{
//耗时操作放在新线程
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(Url).build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
LinkedHashMap<String,Object> hashMaps=stringToJsonObject(responseData);
Message message=new Message();
message.obj=hashMaps;
handler.sendMessage(message);
}catch (Exception e){
e.printStackTrace();
}
}
}).start();
}
/*
* 把Json数据放入hashmap
* @param response
* @return
*/
private LinkedHashMap<String, Object> stringToJsonObject(String response) {
try {
Object json=new JSONTokener(response).nextValue();
if(json instanceof JSONObject){
JSONObject jso = new JSONObject(response);
return JsonObjectToHashMap(jso);
}else{
JSONArray jsa=new JSONArray(response);
return JsonArrayToHashMap(jsa);
}
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
private LinkedHashMap<String, Object> JsonObjectToHashMap(JSONObject jso) {
LinkedHashMap<String, Object> hashmap = new LinkedHashMap<>();
try {
for (Iterator<String> keyStr = jso.keys(); keyStr.hasNext(); ) {
String key1 = keyStr.next().trim();
if (jso.get(key1) instanceof JSONObject) {
JSONObject NextJSONObject=new JSONObject(jso.get(key1).toString());
hashmap.put(key1, JsonObjectToHashMap(NextJSONObject));
}else if(jso.get(key1) instanceof JSONArray) {
JSONArray NextJSONArray =new JSONArray(jso.get(key1).toString());
hashmap.put(key1, JsonArrayToHashMap(NextJSONArray));
}else {
hashmap.put(key1, jso.get(key1));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return hashmap;
}
private LinkedHashMap<String, Object> JsonArrayToHashMap(JSONArray jsa){
LinkedHashMap<String, Object> hashMap = new LinkedHashMap<>();
try {
for (int i = 0; i < jsa.length(); i++) {
if(jsa.get(i) instanceof JSONArray) {
JSONArray NextJSONArray=new JSONArray(jsa.get(i).toString());
hashMap.put(String.valueOf(i), JsonArrayToHashMap(NextJSONArray));
}else if(jsa.get(i) instanceof JSONObject){
JSONObject NextJSONObject=new JSONObject(jsa.get(i).toString());
hashMap.put(String.valueOf(i),JsonObjectToHashMap(NextJSONObject));
}
}
}catch (JSONException e){
e.getStackTrace();
}
return hashMap;
}
}
注释1:因为在网站中提取Json数据是实在线程中完成的,所以在handler中得到hashmap无法回传到调用该方法的类中
(如果有哪位大神指点怎么回传,请多指教),所以handler的操作卸载调用类中。
hashmap中提取数据(JsonForOne.class)
public class JsonForOne {
static Object songs;
public static String getpoint(LinkedHashMap<String, Object> LinkedHashMap, String[] s){
String get=null;
String[] sr=s;
java.util.LinkedHashMap<String ,Object> linked=LinkedHashMap;
for(int i = 0;i<sr.length;i++){
songs=linked.get(sr[i]);
if(songs instanceof java.util.LinkedHashMap){
linked=(java.util.LinkedHashMap<String ,Object>)songs;
continue;
}else {
get=songs.toString();
}
}
return get;
}
}
使用方法
Handler handler=new Handler(){
public void handleMessage(Message msg) {
hashMap=((LinkedHashMap<String, Object>)msg.obj);
Processing(hashMap);
}
};
JsonToHashMap j=new JsonToHashMap("url",handler);
public void Processing(LinkedHashMap<String, Object> map){
//具体操作
String[] name = { "key1值", "key2值"};//key1和key2为嵌套关系
String s = JsonForOne.getpoint(hashMap, name)//简单的Json数据获取
}
以上就是完整版的万能Json数据方法,可大幅的减少代码的重复度
网友评论