maven依赖

<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.5</version>
</dependency>
</dependencies>

读取json

class ReadJSON
{
public static void main(String[] args)
{
String jsonStr = "{ \"one\":\"one\", \"two\":[{ \"two_1_1\":2.11, \"two_1_2\":2.12}, { \"two_2_1\":\"2.21\" } ], \"three\":[\"abc\",false], \"four\":{\"four_1\":4.1, \"four_2\":4.2 } }";
// one:简单类型
// two:对象数组(最复杂)
// three:数组类型
// four:对象类型 jsonGoogle(jsonStr);
jsonAlibaba(jsonStr);
} // gosn读取处理json
public static void jsonGoogle(String jsonStr)
{
JsonParser parser = new JsonParser();
JsonObject jsonObj = (JsonObject) parser.parse(jsonStr); String one = jsonObj.get("one").getAsString();
System.out.println(one);// one JsonArray twoObjArray = jsonObj.get("two").getAsJsonArray();
System.out.println(twoObjArray);// [{"two_1_1":2.11,"two_1_2":2.12},{"two_2_1":"2.21"}]
JsonObject twoObj = (JsonObject) twoObjArray.get(0);
String two = twoObj.get("two_1_1").getAsString();// 可以当成string处理
System.out.println(two);// 2.11 JsonArray threeArray = jsonObj.get("three").getAsJsonArray();
String three_1 = threeArray.get(0).getAsString();
boolean three_2 = threeArray.get(1).getAsBoolean();
System.out.println(three_1 + three_2);// abcfalse JsonObject fourObj = jsonObj.get("four").getAsJsonObject();
double four_1 = fourObj.get("four_1").getAsDouble();
System.out.println(four_1);// 4.1
} // fastjson读取处理json
public static void jsonAlibaba(String jsonStr)
{
JSONObject jsonObj = JSON.parseObject(jsonStr); String one = jsonObj.getString("one");
System.out.println(one);// one JSONArray twoObjArray = jsonObj.getJSONArray("two");
System.out.println(twoObjArray);// [{"two_1_1":2.11,"two_1_2":2.12},{"two_2_1":"2.21"}]
JSONObject twoObj = twoObjArray.getJSONObject(1);
String two_2 = twoObj.getString("two_2_1");
System.out.println(two_2);// 2.21 JSONArray threeArray = jsonObj.getJSONArray("three");
String three_1 = threeArray.getString(0);
boolean three_2 = threeArray.getBoolean(1);
System.out.println(three_1 + three_2);// abcfalse JSONObject fourObj = jsonObj.getJSONObject("four");
String four_1 = fourObj.getString("four_1");
System.out.println(four_1);// 4.1
}
}

写Json

public class Person
{
private String name;
private int age;
private double salary;
private boolean hasBaby;
private List<String> babyNames;
// setter/getter/toString等
}
public class WriteJSON
{
public static void main(String[] args)
{
writeJsonGoogle();
writeJsonAlibaba();
} // gson写json
public static void writeJsonGoogle()
{
JsonObject jsonObj = new JsonObject(); jsonObj.addProperty("one", "oneStr");
jsonObj.addProperty("two", false); JsonObject threeObj = new JsonObject();
threeObj.addProperty("three", 3);
jsonObj.add("three", threeObj); JsonArray jsonArray = new JsonArray();
JsonObject four_1 = new JsonObject();
four_1.addProperty("four_1", 4.1);
JsonObject four_2 = new JsonObject();
four_2.addProperty("four_2", true);
jsonArray.add(four_1);
jsonArray.add(four_2);
jsonObj.add("four", jsonArray); System.out.println(jsonObj.toString());
// {"one":"oneStr","two":false,"three":{"three":3},"four":[{"four_1":4.1},{"four_2":true}]}
} // fastjson写json
public static void writeJsonAlibaba()
{
Map<String, Object> jsonMap = new TreeMap<String, Object>();
jsonMap.put("one", "oneStr");
jsonMap.put("two", false); Map<String, Object> threeObj = new HashMap<String, Object>();
threeObj.put("three_1", "3.1");
threeObj.put("three_2", 3.2);
jsonMap.put("three", threeObj); JSONObject jsonObj = new JSONObject(jsonMap);
List<String> babynames = new ArrayList<String>();
babynames.add("abc");
babynames.add("def");
Person person = new Person("gusi", 12, 7000.00, true, babynames);
jsonObj.put("four", person); jsonObj.put("five", 5); System.out.println(jsonObj.toJSONString());
// {"five":5,"four":{"age":12,"babyNames":["abc","def"],"hasBaby":true,"name":"gusi","salary":7000},"one":"oneStr","three":{"three_1":"3.1","three_2":3.2},"two":false}
}
}

对象类型和json的常用转换

public class Demo implements Serializable
{
String name;
int age;
boolean man; public Demo()
{
super();
} public Demo(String name, int age, boolean man)
{
super();
this.name = name;
this.age = age;
this.man = man;
} // setter/getter,千万不能忘了,不然fastjson不能设置值
@Override
public String toString()
{
return "Demo [name=" + name + ", age=" + age + ", man=" + man + "]";
} }
//gson
Demo demo1 = new Demo("a", 1, false);
String json1 = new Gson().toJson(demo1);// JavaBean到String
System.out.println(json1);
Demo demo2 = new Gson().fromJson(json1, Demo.class);// String到JavaBean
System.out.println(demo2);
JsonObject jsonObj1 = (JsonObject) new JsonParser().parse(json1);// String到jsonObject
System.out.println(jsonObj1);
String json2 = jsonObj1.toString();// jsonObject到String
System.out.println(json2); //fastjson
Demo demo3 = new Demo("b", 2, true);
String json3 = JSON.toJSONString(demo3);// JavaBean到String
System.out.println(json3);
Demo demo4 = JSON.parseObject(json3, Demo.class);// String到JavaBean
System.out.println(demo4);
JSONObject jsonObj2 = JSON.parseObject(json3);// String到jsonObject
System.out.println(jsonObj2);
String json4 = jsonObj2.toJSONString();// jsonObject到String
System.out.println(json4);
JSONObject jsonObj3 = (JSONObject) JSON.toJSON(demo3);// JavaBean到jsonObject
System.out.println(jsonObj3);

java json的处理的更多相关文章

  1. java json 的生成和解析 --json-lib

    类(java json的解析和生成): import java.util.HashMap; import java.util.Map; import net.sf.json.JSONArray; im ...

  2. Java json设置时间格式,Jackson设置时间格式,json设置单引号

    Java json设置时间格式,Jackson设置时间格式,json设置单引号 >>>>>>>>>>>>>>> ...

  3. Java json工具类,jackson工具类,ObjectMapper工具类

    Java json工具类,jackson工具类,ObjectMapper工具类 >>>>>>>>>>>>>>> ...

  4. java json和对象互相装换

    java json和对象互相装换 1.com.alibaba.fastjson.JSON 2.com.fasterxml.jackson.databind.ObjectMapper

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

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

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

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

  7. Java JSON数据创建和读取

    Java  json数据创建 package com.JavaTest; import com.google.gson.JsonArray; import com.google.gson.JsonOb ...

  8. Java Json格式的字符串转变对象

    Java Json格式的字符串转变对象: 方法还是比较多的: 学习:https://my.oschina.net/heweipo/blog/386808 其中的jsonlib说明:http://www ...

  9. Java json串生成及转bean

      package com; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import j ...

  10. java -json()

    json-lib和org.json的使用几乎是相同的,我总结出的区别有两点: 两种包 1. List集合转换成json方法 List list = new ArrayList(); list.add( ...

随机推荐

  1. python手记(36)

    #!/usr/bin/env python #-*- coding: utf-8 -*- #code:myhaspl@qq.com import cv2 import numpy as np fn=& ...

  2. ASP.NET导出EXCEl方法使用EXCEl对象

    导出功能必须使用  office中EXCEl对象,整个操作如同在操作EXCEl一样,建立EXCEl应用----建立工作簿---建立sheet表单页, 代码实现过程中,如果想对单元格实现一些操作,或者汇 ...

  3. 复习一下sql server的inner join left join 和right join

    1.left join sql语句如下: select * from A left join B  on A.aID = B.bID 结果如下:aID               aNum       ...

  4. html基础标签-1-pre预格式标签

    pre预格式标签 code,tt标签 1 <!doctype html> 2 <html lang='zh-cn'> 3 <head> 4 <meta cha ...

  5. su Authentication failure解决

    su Authentication failure解决 关于Ubuntu桌面系统su root时认证失败的问题 1. Ubuntu 默认没有给root用户设置密码,当我们su root命令时, 提示认 ...

  6. fatal error LNK1112: module machine type 'X86' conflicts with target machine type 'x64'

    xxxxxx.lib(xxxxxx.obj) : fatal error LNK1112: module machine type 'X86' conflicts with target machin ...

  7. html object元素

    知道object是播放音频,但是想了解具体点,百度一下,感觉模模糊糊的,感觉看不大明白,最后找到一个解释比较详细,先从应用,到解释具体属性, 具体网址是: http://www.w3school.co ...

  8. SnappyDB—Android上的NoSQL数据库简介

    参考:http://www.open-open.com/lib/view/open1420816891937.html 参考:http://android-arsenal.com/details/1/ ...

  9. Android 汉字转拼音之JNI篇

    package com.tool.hz2py; import android.os.Bundle; import android.app.Activity; import android.view.M ...

  10. python 学习之Windows 下的编码处理!

    问题1: Non-ASCII character '\xe9' in file 问题原因:程序编码上出现问题 解决方法:在程序头部加上代码 #-*- coding: UTF-8 -*- 设置代码编码为 ...