且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何阅读在Java中使用Gson的Json

更新时间:2023-11-26 09:44:28

JSON 是JavaScript Object Notation的缩写,是一种存储信息以有组织,易于访问的方式进行。简而言之,它为我们提供了一个可读的数据集合,我们可以通过一种非常合理的方式访问这些数据。



作为一个简单的例子,关于我的信息可能会写入如下所示:

  {
age:24,
hometown: Missoula,MT,
gender:male
}

要使用 Gson 读取java中的 Json 文件,您需要使用 Gson lib。



Gson是一个Java库,可用于将Java对象转换为他们的JSON表示。它也可以用来将JSON字符串转换为等效的Java对象。 Gson可以使用任意Java对象,包括您没有源代码的预先存在的对象。


$ b 演示:从 file.json,转换回对象并显示它。
  package com.mkyong.core; 

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.google.gson.Gson;

public class GsonExample {
public static void main(String [] args){

Gson gson = new Gson();

try {

BufferedReader br = new BufferedReader(
new FileReader(c:\\file.json));

//将json字符串转换回对象
DataObject obj = gson.fromJson(br,DataObject.class);

System.out.println(obj);

} catch(IOException e){
e.printStackTrace();
}

}
}


I'm required to read a Json file, using the Gson library, in Java. First I don't understand what a Json file is, what I have is the following: (sorry for the lack of spaces, that's how it was pasted)

{
"initialStorage": [
{shoeType: "red-boots", amount: 10},
{shoeType: "green-flip-flops", amount: 7}
],
services: {
time: {
speed: 1000,
duration: 24
},
manager: {
discountSchedule: [
{shoeType: "red-boots", amount: 1, tick: 3},
{shoeType: "green-flip-flops", amount: 3, tick:10}
]
},
factories: 3,
sellers: 2,
customers: [
{
name: "Bruria",
wishList: ["green-flip-flops"],
purchaseSchedule: [
{shoeType: "red-boots", tick: 3}
]
},
{
name: "Shraga",
wishList: [],
purchaseSchedule: [
{shoeType: "green-flip-flops", tick: 12}
]
}
]
}
}

  1. Do I just put it in java as.. String json = "-pasting it here-"? What is a "Json" file, is it some sort of a file I have to read as nameoffile.json?
  2. How do I use Gson to read it? I didn't understand much from the *** videos explaining it.

The following link shows the given Json input in a more comfortable way: http://i.stack.imgur.com/r0cTs.png

JSON is short for JavaScript Object Notation, and is a way to store information in an organized, easy-to-access manner. In a nutshell, it gives us a human-readable collection of data that we can access in a really logical manner.

As a simple example, information about me might be written in JSON as follows:

{
    "age" : "24",
    "hometown" : "Missoula, MT",
    "gender" : "male"
}

To Read the Json file in java using Gson you need to use Gson lib.

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.

Demo : Read data from "file.json", convert back to object and display it.

package com.mkyong.core;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.google.gson.Gson;

public class GsonExample {
    public static void main(String[] args) {

    Gson gson = new Gson();

    try {

        BufferedReader br = new BufferedReader(
            new FileReader("c:\\file.json"));

        //convert the json string back to object
        DataObject obj = gson.fromJson(br, DataObject.class);

        System.out.println(obj);

    } catch (IOException e) {
        e.printStackTrace();
    }

    }
}