java json的处理

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);
时间: 2024-07-31 14:33:02

java json的处理的相关文章

Java JSON数据创建和读取

Java  json数据创建 package com.JavaTest; import com.google.gson.JsonArray; import com.google.gson.JsonObject; public class CreatJson { public static void main(String[] args) { JsonObject object = new JsonObject(); object.addProperty("cat", "it&

Java Json库的选取准则

为公司做了小任务,需要用到Java Json库,Json库我几个月之前就用过,不过那时候是跟着项目来的,延续了项目的使用习惯直接用了jackson Json,而这次我觉得好好比较一下几个常见的Json库,然后选一个最快的. 看了几篇blog,觉得其实可选的就三种,jackson, gson, json.simple.我最初用了json.simple,然后写出来了这样的函数 从URL中读取Json数据,并且在Request中添加身份信息 public static JSONObject readJ

【代码分享——Java&amp;Json】Json转成java对象,已经java对象转成Json对象

做记录用,肯定有地方不完整,先放着吧 [代码分享--Java&Json]Json转成java对象,已经java对象转成Json对象,布布扣,bubuko.com

Java+Json+JQuery将本地文件显示在网页上

前段时间为是练习JQuery和Java遍历目录,写了一个JavaWeb(使用Jsp+Servlet)的例子.源代码下载:https://github.com/liaoyu/uudisk 上述源码是Myeclipse新建的项目,需要配置一些环境,比如JRE路径,以下是运行截图,界面模仿新浪微盘 工作原理就是通过Java遍历系统(Windows)的目录,前台通过点击图标以ajax方式触发事件,后台以json数据的形式把文件结构返回给前台,前台通过JS解析JSON数据内容,展示不同的图片.目前尚存在的

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

类(java json的解析和生成): import java.util.HashMap; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JsonTest { public String jsonToString(){ JSONObject users = new JSONObject(); JSONArray array = new JSONArra

Java json解析gson总结

gson官网 https://github.com/google/gson, https://github.com/google/gson/releases 官网没找到jar包下载点,根据源码在jdk1.6环境下生成一份jar包. 因为屏蔽问题无法访问,上传于此 便于大家下载使用. http://download.csdn.net/detail/ronghua_liu/8918459 --------------------------------------------------------

Java Json 序列化与反序列化

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集. JSON采用完全独立于语言的文本格式,这些特性使JSON成为理想的数据交换语言. 下面介绍两款自己在开发中常用的处理Json的Java开源类库:Gson和Fastjson 1.Gso

java Json.org.jar

servlet我们可以把他当成一个数据媒介,他最终执行的是将方法体内获取处理后的数据,返回给请求的客户端,或以XML格式,或以JSON格式 ,我这里是使用的JSON格式数据,所以下面我要说org.json.jar这个库及我封装的返回数据的方式. 这个库有两个核心类->JsonObject 和JsonArray 一.JsonObject  JsonObject 这个类就相当于IOS中的NSDictionary,转换后是以键值对的方式将数据呈现给客户端调用人员,当然这里我们返回的是JSON字符,所以

Java &quot;JSON中无分隔符日期字符串处理&quot;

Json 中日期类型数据处理,服务端传输的日期没有分隔符,一般格式就两种,[20151212121212]即yyyyMMddhhmmss和[121212]hhmmss import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; public class test { public static void main(String[] arg

Java Json API:Gson使用简单入门

GSON是Google开发的Java API,用于转换Java对象和Json对象.本文讨论并提供了使用API的简单代码示例.更多关于GSON的API可以访问:http://sites.google.com/site/gson/. 本文是GSON系列文章的第一篇.本文是其他文章的基础,因此不需要任何GSON或JSON经验.第二篇文章提供了关于GSON反序列化(从JSON到Java)的示例,第三篇文章提供了关于GSON序列化(从Java到JSON)的示例. 下面列出的所有代码都可以在https://