如何在 Java 中创建特定格式的 JSON 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19964464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a JSON file in a particular format in Java
提问by user2864315
I am trying to write a JSON file using Java in a particular format. For example: let's suppose I like to get the JSON file written in the following format:
我正在尝试使用 Java 以特定格式编写 JSON 文件。例如:假设我喜欢以以下格式编写 JSON 文件:
{
"resource":[{"name":"Node1"}],
"literals":[{"literal":"A", "B", "C", "D"}]
}
As you may notice, in resource
, I would like to write strings in there and in literals
, I like to have an arraylist of strings. Here are my Java codes:
您可能会注意到,在 中resource
,我想在其中写入字符串,而在 中literals
,我喜欢有一个字符串数组列表。这是我的Java代码:
public void writeJSON() {
public ArrayList<String> literals = new ArrayList<String>();
literals.add("A");
literals.add("B");
literals.add("C");
literals.add("D");
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
jsonObject.put("name", resources.getResource());
resources.setLiterals(literals);
jsonObject.put("literal", resources.getLiterals());
try {
FileWriter file = new FileWriter("/Users/Documents/sample.json");
file.write(jsonObject.toJSONString());
file.flush();
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Here is the Java file where I set and get values for the JSON file:
这是我设置和获取 JSON 文件值的 Java 文件:
public class Resources {
String resource;
ArrayList<String> literals;
public Resources()
{
}
public void setResource(String resource)
{
this.resource = resource;
}
public String getResource()
{
return resource;
}
public void setLiterals(ArrayList<String> literals)
{
this.literals = literals;
}
public ArrayList<String> getLiterals()
{
return literals;
}
}
Could anyone please help me how to create a JSON file based on the format provided above. Your help would be very much appreciated.
任何人都可以帮助我如何根据上面提供的格式创建一个 JSON 文件。您的帮助将不胜感激。
采纳答案by Алексей
Lets start that Json
you are trying to create is invalid.
This is what you have:
让我们开始,Json
你试图创建是无效的。这是你所拥有的:
{
"resource":[{"name":"Node1"},
{"name":"Node2"}],
"literals":[{"source":"A", "B", "C", "D"},
{"source":"E", "F", "G", "H"}]
}
As you can see resource
looks OK - i.e. it is an object that contains List
of objects.
On the other hand literals
is an object that contain a List
of what? since not {"source":"A", "B", "C", "D"}
nor {"source":"E", "F", "G", "H"}
is a valid Json
.
如您所见,resource
看起来不错 - 即它是一个包含List
对象的对象。另一方面literals
是一个包含List
什么的对象?因为 not{"source":"A", "B", "C", "D"}
也不{"source":"E", "F", "G", "H"}
是一个有效的Json
.
Figure out this first, then update your java
code and we will go from there.
首先弄清楚这一点,然后更新您的java
代码,我们将从那里开始。
If this: {"resource":[{"name":"Node1"}],"literals":[{"literal":["A","B","C","D"]}]}
is desirable, then you can achieve that in the following way(I will use Gson):
如果这:{"resource":[{"name":"Node1"}],"literals":[{"literal":["A","B","C","D"]}]}
是可取的,那么您可以通过以下方式实现(我将使用Gson):
JsonWriter jsonWriter = null;
try {
jsonWriter = new JsonWriter(new FileWriter("test.json"));
jsonWriter.beginObject();
jsonWriter.name("resource");
jsonWriter.beginArray();
jsonWriter.beginObject();
jsonWriter.name("name");
jsonWriter.value("Node1");
jsonWriter.endObject();
jsonWriter.endArray();
jsonWriter.name("literals");
jsonWriter.beginArray();
jsonWriter.beginObject();
jsonWriter.name("literal");
jsonWriter.beginArray();
jsonWriter.value("A");
jsonWriter.value("B");
jsonWriter.value("C");
jsonWriter.value("D");
jsonWriter.endArray();
jsonWriter.endObject();
jsonWriter.endArray();
jsonWriter.endObject();
} catch (IOException e) {
...
}finally{
try {
jsonWriter.close();
} catch (IOException e) {
...
}
}