栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

基于java解析JSON的三种方式详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于java解析JSON的三种方式详解

本文实例分析了基于java解析JSON的三种方式。分享给大家供大家参考,具体如下:

一、什么是JSON?

JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能力却不差,由于它的小巧所以网络传输数据将减少更多流量从而加快速度。

JSON就是一串字符串 只不过元素会使用特定的符号标注。

{} 双括号表示对象
[] 中括号表示数组
"" 双引号内是属性或值
: 冒号表示后者是前者的值(这个值可以是字符串、数字、也可以是另一个数组或对象)

所以 {"name": "Michael"} 可以理解为是一个包含name为Michael的对象

而[{"name": "Michael"},{"name": "Jerry"}]就表示包含两个对象的数组

当然了,你也可以使用{"name":["Michael","Jerry"]}来简化上面一部,这是一个拥有一个name数组的对象

二、JSON解析之传统的JSON解析

1、生成json字符串

public static String createJsonString(String key, Object value) {
    JSonObject jsonObject = new JSonObject();
    jsonObject.put(key, value);
    return jsonObject.toString();
}

2、解析JSON字符串

分为以下三种情况,一个JavaBean,一个List数组,一个嵌套Map的List数组:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import com.android.myjson.domain.Person;

public class JsonTools {
  public static Person getPerson(String key, String jsonString) {
    Person person = new Person();
    try {
      JSonObject jsonObject = new JSonObject(jsonString);
      JSonObject personObject = jsonObject.getJSonObject("person");
      person.setId(personObject.getInt("id"));
      person.setName(personObject.getString("name"));
      person.setAddress(personObject.getString("address"));
    } catch (Exception e) {
      // TODO: handle exception
    }
    return person;
  }
  public static List getPersons(String key, String jsonString) {
    List list = new ArrayList();
    try {
      JSonObject jsonObject = new JSonObject(jsonString);
      // 返回json的数组
      JSonArray jsonArray = jsonObject.getJSonArray(key);
      for (int i = 0; i < jsonArray.length(); i++) {
 JSonObject jsonObject2 = jsonArray.getJSonObject(i);
 Person person = new Person();
 person.setId(jsonObject2.getInt("id"));
 person.setName(jsonObject2.getString("name"));
 person.setAddress(jsonObject2.getString("address"));
 list.add(person);
      }
    } catch (Exception e) {
      // TODO: handle exception
    }
    return list;
  }
  public static List getList(String key, String jsonString) {
    List list = new ArrayList();
    try {
      JSonObject jsonObject = new JSonObject(jsonString);
      JSonArray jsonArray = jsonObject.getJSonArray(key);
      for (int i = 0; i < jsonArray.length(); i++) {
 String msg = jsonArray.getString(i);
 list.add(msg);
      }
    } catch (Exception e) {
      // TODO: handle exception
    }
    return list;
  }
  public static List> listKeyMaps(String key,
      String jsonString) {
    List> list = new ArrayList>();
    try {
      JSonObject jsonObject = new JSonObject(jsonString);
      JSonArray jsonArray = jsonObject.getJSonArray(key);
      for (int i = 0; i < jsonArray.length(); i++) {
 JSonObject jsonObject2 = jsonArray.getJSonObject(i);
 Map map = new HashMap();
 Iterator iterator = jsonObject2.keys();
 while (iterator.hasNext()) {
   String json_key = iterator.next();
   Object json_value = jsonObject2.get(json_key);
   if (json_value == null) {
     json_value = "";
   }
   map.put(json_key, json_value);
 }
 list.add(map);
      }
    } catch (Exception e) {
      // TODO: handle exception
    }
    return list;
  }
}

三、JSON解析之GSON

1、生成JSON字符串

import com.google.gson.Gson;
public class JsonUtils {
  public static String createJsonObject(Object obj) {
    Gson gson = new Gson();
    String str = gson.toJson(obj);
    return str;
  }
}

2、解析JSON

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
;
public class GsonTools {
  public GsonTools() {
    // TODO Auto-generated constructor stub
  }
  
  public static T getPerson(String jsonString, Class cls) {
    T t = null;
    try {
      Gson gson = new Gson();
      t = gson.fromJson(jsonString, cls);
    } catch (Exception e) {
      // TODO: handle exception
    }
    return t;
  }
  
  public static List getPersons(String jsonString, Class cls) {
    List list = new ArrayList();
    try {
      Gson gson = new Gson();
      list = gson.fromJson(jsonString, new TypeToken>() {
      }.getType());
    } catch (Exception e) {
    }
    return list;
  }
  
  public static List getList(String jsonString) {
    List list = new ArrayList();
    try {
      Gson gson = new Gson();
      list = gson.fromJson(jsonString, new TypeToken>() {
      }.getType());
    } catch (Exception e) {
      // TODO: handle exception
    }
    return list;
  }
  public static List> listKeyMaps(String jsonString) {
    List> list = new ArrayList>();
    try {
      Gson gson = new Gson();
      list = gson.fromJson(jsonString,
   new TypeToken>>() {
   }.getType());
    } catch (Exception e) {
      // TODO: handle exception
    }
    return list;
  }
}

四、JSON解析之FastJSON

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
public class JsonTool {
  public static T getPerson(String jsonstring, Class cls) {
    T t = null;
    try {
      t = JSON.parseObject(jsonstring, cls);
    } catch (Exception e) {
      // TODO: handle exception
    }
    return t;
  }
  public static List getPersonList(String jsonstring, Class cls) {
    List list = new ArrayList();
    try {
      list = JSON.parseArray(jsonstring, cls);
    } catch (Exception e) {
      // TODO: handle exception
    }
    return list;
  }
  public static List> getPersonListMap1(
      String jsonstring) {
    List> list = new ArrayList>();
    try {
      list = JSON.parseObject(jsonstring,
   new TypeReference>>() {
   }.getType());
    } catch (Exception e) {
      // TODO: handle exception
    }
    return list;
  }
}

总结:

JSON对于移动设备来说,尤其对于网络环境较差和流量限制的情况下,相对于XML格式的数据传输会更节省流量,传输效率更高。在这三种解析方式中FastJson是效率最高的,推荐使用。

PS:关于json操作,这里再为大家推荐几款比较实用的json在线工具供大家参考使用:

在线JSON代码检验、检验、美化、格式化工具:
http://tools.jb51.net/code/json

JSON在线格式化工具:
http://tools.jb51.net/code/jsonformat

在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson

json代码在线格式化/美化/压缩/编辑/转换工具:
http://tools.jb51.net/code/jsoncodeformat

在线json压缩/转义工具:
http://tools.jb51.net/code/json_yasuo_trans

C语言风格/HTML/CSS/json代码格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json

希望本文所述对大家java程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/148263.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号