如何阅读在Java中使用Gson的Json [英] How to read Json using Gson in java
问题描述
我需要使用Java中的Gson库来读取Json文件。首先,我不明白Json文件是什么,我有以下内容:(抱歉缺少空格,就是它被粘贴的方式)
{
initialStorage:[
{shoeType:red-boots,amount:10},
{shoeType:green-flip-flops,金额:7}
],
服务:{
时间:{
速度:1000,
持续时间:24
},
经理:{
discountSchedule:[
{shoeType:red-boots,amount:1,tick:3},
{shoeType:green-flip-flops,amount:3, tick:10}
]
},
工厂:3,
卖家:2,
customers:[
{
name: Bruria,
wishlist:[green-flip-flops],
purchaseSchedule:[
{shoeType:red-boots,tick:3}
]
$,
{
name:Shraga,
wishList:[],
purchaseSchedule:[
{shoeType:green-flip-flops, tick:12}
]
}
]
}
}
- 我只是把它放在java中作为字符串json =-pasting it here-?什么是Json文件,它是一种我必须读作nameoffile.json的文件吗?
- 如何使用Gson来读取它?我对从解释它的youtube视频中了解不多。 舒适的方式: http://i.stack.imgur.com/r0cTs.png
- 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?
- How do I use Gson to read it? I didn't understand much from the youtube videos explaining it.
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}
]
}
]
}
}
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();
}
}
}
这篇关于如何阅读在Java中使用Gson的Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!