zoukankan      html  css  js  c++  java
  • Gson

    1、commons-beanutils.jar

    2、commons-collections-3.2.jar

    3、commons-lang-2.5.jar

    4、commons-logging-1.1.1.jar

    5、ezmorph-1.0.4.jar

    6、json-lib-2.4-jdk15.jar

    一、在 js 中的 json 数据 转换

      1.  json 字符串 转换成 Gson 结构

        JSON.parse(json字符串)

      2. Gson 结构 转换成 json 字符串

        JSON.stringify(Gson结构)

      举例:

    <script type="text/javascript" language="javascript">
        var strJson = '{"str":"aa"}';
        // json 字符串 转换成 Gson 结构
        var gson = JSON.parse(strJson);
        // Gson 结构 转换成 json 字符串
        var json = JSON.stringify(gson)
    </script>

    二、在java 中 json 数据 转换

      1. Map 转换成 json 字符串

    <dependency>
      <groupId>net.sf.json-lib</groupId>
      <artifactId>json-lib</artifactId>
      <version>2.4</version>
      <classifier>jdk15</classifier>
    </dependency>

    import net.sf.json.JSONObject;

    Map<String,Object> jsonRs =new HashMap<String,Object>();
    JSONObject jsonObject = JSONObject.fromObject(jsonRs);
    String strJson = jsonObject .toString(); 

      2. List 转换成 json  

    <dependency>
      <groupId>net.sf.json-lib</groupId>
      <artifactId>json-lib</artifactId>
      <version>2.4</version>
      <classifier>jdk15</classifier>
    </dependency>

    import net.sf.json.JSONArray;

    List<Map<String,Object>> jsonLs= new ArrayList<Map<String,Object>>();
    JSONArray jSONArray = JSONArray.fromObject(jsonLs);
    String strJson = jSONArray .toString();

      3.  json 解析

    import com.google.gson.JsonArray;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParser;

    String strGson = 获取到的json字符串;
    JsonParser parser = new JsonParser();
    
    JsonElement El_Record = parser.parse(strGson);
    if(El_Record.isJsonObject()){
        JsonObject Obj_Recode = El_Record.getAsJsonObject();  
    }
    //
    JsonElement El_zb = Obj_Recode.get("tdh_zb");
    if(El_zb.isJsonObject()){
        JsonObject Obj_zb = El_zb.getAsJsonObject();
        num_zb = Obj_zb.get("num").getAsInt();
        JsonElement El_zb_indexAll = Obj_zb.get("indexAll");
        Arr_zb_indexAll = El_zb_indexAll.getAsJsonArray();
        if(Arr_zb_indexAll!=null && Arr_zb_indexAll.size()>0){
            for(JsonElement El_zb_index : Arr_zb_indexAll){
            JsonObject Obj_zb_index = El_zb_index.getAsJsonObject();
            String zb_index = Obj_zb_index.get("index").getAsString();
        }        
    }               
  • 相关阅读:
    团队-团队编程项目作业名称-最终程序
    《团队-中国象棋-项目总结》
    课后作业-阅读任务-阅读提问-4
    《20171130-构建之法:现代软件工程-阅读笔记4》
    《软件工程课程总结》
    《20171117-构建之法:现代软件工程-阅读笔记-3》
    -课后作业-阅读任务-阅读提问-3
    《团队-中国象棋-团队一阶段互评》
    《团队-中国象棋-开发文档》
    《结对-贪吃蛇-结对项目总结》
  • 原文地址:https://www.cnblogs.com/MissRabbit/p/6140355.html
Copyright © 2011-2022 走看看