下载引入包gson-2.0.jar

1.字符转数据

final Map map = new HashMap();
map.put("contents",
    "[{\"Id\":\"111\",\"AcceptNum\":222,\"ArchiveNum\":333,\"ArchivRate\":444,\"CUR_DATA_DT\":555},{\"Id\":\"112\",\"AcceptNum\":222,\"ArchiveNum\":333,\"ArchivRate\":444,\"CUR_DATA_DT\":555}]");
map.put("footer", "{\"Id\":\"总合计\",\"AcceptNum\":151886,\"ArchiveNum\":449135}");
map.put("fields", "Id,AcceptNum,ArchiveNum,ArchivRate,CUR_DATA_DT"); String[] fields = {};// excel字段
String tempContent="";
JsonArray listArray = new JsonArray();
JsonObject object = new JsonObject();
JsonObject obj = new JsonObject();
obj = listArray.get(1).getAsJsonObject();
listArray = new JsonParser().parse(map.get("contents").toString()).getAsJsonArray();//json数组
  if (map.containsKey("footer") && !map.get("footer").toString().trim().equals("")) {
   footerJson = new JsonParser().parse(map.get("footer").toString()).getAsJsonObject();//json对象
   map.remove("footer");
  }
fields=map.get("fields").toString().trim().split(",", -1);//普通数组
tempContent= obj.get("Id").toString().replace("\"", "");//json存取

2.构造json对象

JsonObject object = new JsonObject();
// add the key/value property
object.addProperty("code", "0");
object.addProperty("msg", "error");
JsonArray listArray = new JsonArray();
JsonObject Row = new JsonObject(); for(int rownum=1;rownum<5;rownum++){

    for (int colnum = 1; colnum <= 2; colnum++) {      // insert the cell(columnName/columnValue) into the row

      Row.addProperty("col"+colnum, "val"+colnum);

    }

// insert one row into listTable "List"

  listArray.add(Row);

}

JsonObject subObject = new JsonObject();

subObject.add("List", listArray);

subObject.addProperty("page", "1");
subObject.addProperty("page_size", "10");
subObject.addProperty("total_count", "25");
subObject.addProperty("page_count", "25");

object.add("result", subObject);

System.out.println(object.toString());

{"code":"0","msg":"error","result":{"page":"1","page_size":"10","total_count":"25","page_count":"25","List":[{"col1":"val1","col2":"val2"},{"col1":"val1","col2":"val2"},{"col1":"val1","col2":"val2"},{"col1":"val1","col2":"val2"},{"col1":"val1","col2":"val2"}]}}

java json字符串与对象转换的更多相关文章

  1. java json字符串和对象互转

    /** * Created by admin on 2017/7/26. */ public class NewPost { private String title; private String ...

  2. <摘录>Gson对Java嵌套对象和JSON字符串之间的转换

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,具有良好的跨平台特性.近几年来已经和XML一样成为C/S架构中广泛采用的数据格式.有关JSON的更多知识, ...

  3. java对象与json字符串的互相转换

    java对象与json字符串的互相转换 1.采用 net.sf.json.JSONObject maven依赖包: <dependency> <groupId>net.sf.j ...

  4. 使用Google的Gson实现对象和json字符串之间的转换

    使用Google的Gson实现对象和json字符串之间的转换 需要gson.jar 1.JsonUtil.java package com.snail.json; import java.lang.r ...

  5. json对象和json字符串之间的转换-JavaScript实现

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  6. js中json字符串与对象的转换及是否为空

    1.json对象(数组)转字符串 var b=[ { "CategoryName" : "Beverages", "ProductName" ...

  7. java json与map互相转换(二)

      java json与map互相转换(二) CreationTime--2018年7月16日15点09分 Author:Marydon 1.准备工作 所需jar包: commons-beanutil ...

  8. json字符串和对象的相互转化

    json在代码中是经常用到的,在此总结一下json字符串和对象及数组之间的相互转化: 1.javascript函数方式: <1> JSON.stringify :把一个对象转换成json字 ...

  9. java json与map互相转换(一)

      java json与map互相转换(一) CreationTime--2018年7月16日 Author:Marydon 1.准备工作 所需jar包:json-20180130.jar impor ...

随机推荐

  1. 学习笔记之pandas Foundations | DataCamp

    pandas Foundations | DataCamp https://www.datacamp.com/courses/pandas-foundations Many real-world da ...

  2. MATLAB的一些小技巧

    写论文要将图片保存为tiff格式,还要求dpi,还要标注,真是麻烦,下面的命令是最方便的程序化处理方式了 MATLAB text标注后 保存为 tiff 图片,图片到边框间无空白 clear all; ...

  3. go语言学习--channel的关闭

    在使用Go channel的时候,一个适用的原则是不要从接收端关闭channel,也不要在多个并发发送端中关闭channel.换句话说,如果sender(发送者)只是唯一的sender或者是chann ...

  4. [UE4]加入音效

  5. [UE4]暂停游戏、退出游戏、游戏输入模式

    游戏主界面WB_Main蓝图 Set Game Paused:暂停游戏 Show Mouse Cursor:显示鼠标 Set Input Mode:设置游戏输入模式(游戏和UI).仅仅游戏.仅仅UI( ...

  6. Oracle 在SQL语句中如何获取系统当前时间并进行操作

    select sysdate from dual;select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual; select to_char(s ...

  7. typescript接口的概念 以及属性类型接口

    /* 1.vscode配置自动编译 1.第一步 tsc --inti 生成tsconfig.json 改 "outDir": "./js", 2.第二步 任务 ...

  8. delphi打开项目提示unable to find resource on dll projects

    用记事本打开*.dof文件, 把这行[Resource DLL Projects]及它的所属的内容删除就行了.

  9. for练习.html

    <script> 偶数 var str=""; for (var i = 1 ; i <= 100; i++){ if (i%2 == 0) { //str = ...

  10. (转)C# Oracle数据库操作类

    原文地址:http://www.cnblogs.com/haibing0107/p/6143922.html using System;using System.Data;using System.C ...