jackson Read 读取 json 文件和 Write 写入 json 文件 读写 json 文件

jackson Read Write 读取 json 文件和写入 json 文件

jackson 可以由这里获得 http://repo1.maven.org/maven2/com/fasterxml/jackson/

有时候需要将 json 写入磁盘或者由此片读取,这里提供两个例子:创建json对象并写入文件、读取 json 文件并转换为 json 对象,例子如下:

创建 JSON 对象,并写入磁盘文件 c:/temp/test.json

package test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.FileWriter;
import java.io.IOException;

public class WriteJSON
{

  public WriteJSON()
  {

  }

  public static void main(String[] args)
  {
    try
    {
      ObjectMapper mapper = new ObjectMapper();

      // Create the root node
      ObjectNode root = mapper.createObjectNode();
      // Create a child node
      ObjectNode node1 = mapper.createObjectNode();
      node1.put("nodekey1", 1);
      node1.put("nodekey2", 2);
      // Bind the child nodes
      root.set("child", node1);
      // Array of nodes
      ArrayNode arrayNode = mapper.createArrayNode();
      arrayNode.add(node1);
      arrayNode.add(1);
      // Bind array node
      root.set("arraynode1", arrayNode);
      root.set("arraynode2", arrayNode);

      try (FileWriter file = new FileWriter("c:/temp/test.json"))
      {
        file.write(root.toString());
        file.flush();
      }
      catch (IOException e)
      {
        e.printStackTrace();
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
}

 

数据以 json 格式存储在文件 c:/temp/data.json 中,用 jackson 的 ObjectMapper 对象载入,用 JsonNode 对象即可进行读取或者存取操作。

c:/temp/data.json 文件内容:

{"data":{"birth_day":7,"birth_month":6},"errcode":0,"msg":"ok","ret":0}

java 代码如下:

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ReadJSON
{

  public static void main(String[] args)
  {
    getJsonFile();

  }

  public static String getJsonFile()
  {
    String strResult = "";
    ObjectMapper objMapper = new ObjectMapper();

    try
    {
      // 指定文件
      String strText = "c:/temp/test.json";
      JsonNode rootNode = objMapper.readTree(new File(strText));
      // 指定某一个节点
      String strData = rootNode.get("child").toString();

      System.out.println(rootNode.toString());
      System.out.println(strData);

      // 获得 json 字符串
      strResult = rootNode.toString();
    }
    catch (JsonProcessingException e)
    {
      e.printStackTrace();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }

    return strResult;
  }

}

控制台输出结果:

{"data":{"birth_day":7,"birth_month":6},"errcode":0,"msg":"ok","ret":0}
{"birth_day":7,"birth_month":6}

 

 

 

blog.csdn.net/joyous/article/details/50532149

Q群236201801讨论

 

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
使用Jackson读取JSON文件的步骤如下: 1. 引入Jackson库 在项目中引入Jackson库,如果是Maven项目可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.12.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.2</version> </dependency> ``` 2. 创建ObjectMapper对象 ObjectMapper是Jackson库中的一个类,用于将JSON字符串转换为Java对象或将Java对象转换为JSON字符串。 ```java ObjectMapper objectMapper = new ObjectMapper(); ``` 3. 读取JSON文件 使用ObjectMapper对象的readValue()方法读取JSON文件,该方法的第一个参数为JSON文件的InputStream对象,第二个参数为要转换成的Java对象的类型。 ```java File file = new File("example.json"); InputStream inputStream = new FileInputStream(file); MyClass myClass = objectMapper.readValue(inputStream, MyClass.class); ``` 其中MyClass为要转换成的Java对象的类型。 完整的读取JSON文件的示例代码如下: ```java import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; public class JsonReader { public static void main(String[] args) throws Exception { ObjectMapper objectMapper = new ObjectMapper(); File file = new File("example.json"); InputStream inputStream = new FileInputStream(file); MyClass myClass = objectMapper.readValue(inputStream, MyClass.class); System.out.println(myClass); } } class MyClass { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "MyClass{" + "name='" + name + '\'' + ", age=" + age + '}'; } } ``` 其中example.json文件内容为: ```json { "name": "John", "age": 30 } ``` 输出结果为: ``` MyClass{name='John', age=30} ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值