java 如何使用 Jackson 创建这个 json 字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12594095/
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 this json string using Hymanson?
提问by Andy
Here's my desired output
这是我想要的输出
{"node":{"type":"community","field_incentives":{"und":[{"value":"fun"},{"value":"nice"}]},"field_community_email":{"und":[{"value":"[email protected]"}]}}}
Here's my code but it does not seem to generate the output above. If there's a better and simpler way to do this, please let me know. Thanks
这是我的代码,但它似乎没有生成上面的输出。如果有更好更简单的方法来做到这一点,请告诉我。谢谢
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonFactory f = new JsonFactory();
JsonGenerator g = f.createJsonGenerator(outputStream);
g.writeStartObject();
g.writeObjectFieldStart("node");
g.writeStringField("type", "community");
g.writeObjectFieldStart("field_incentives");
g.writeFieldName("und");
g.writeStartArray();
???I don't know how to make [{"value":"fun"},{"value":"nice"}]
g.writeEndArray();
g.writeEndObject();
g.close();
回答by swemon
I simply write line by line to your output json file Reference JsonGenerator. Hope it will help.
我只是逐行写入您的输出 json 文件Reference JsonGenerator。希望它会有所帮助。
import java.io.File;
import java.io.IOException;
import org.codehaus.Hymanson.JsonEncoding;
import org.codehaus.Hymanson.JsonFactory;
import org.codehaus.Hymanson.JsonGenerationException;
import org.codehaus.Hymanson.JsonGenerator;
import org.codehaus.Hymanson.map.JsonMappingException;
public class CopyOfHymansonStreamExample {
public static void main(String[] args) {
try {
JsonFactory jfactory = new JsonFactory();
/*** write to file ***/
JsonGenerator jGenerator = jfactory.createJsonGenerator(new File(
"c:\user.json"), JsonEncoding.UTF8);
jGenerator.writeStartObject(); // {
jGenerator.writeObjectFieldStart("node"); // node: {
jGenerator.writeStringField("type", "community"); // "type" : "community"
jGenerator.writeObjectFieldStart("field_incentives"); // "field_incentives" : {
jGenerator.writeFieldName("und"); // "und" :
jGenerator.writeStartArray(); // [
jGenerator.writeStartObject(); // {
jGenerator.writeStringField("value", "fun"); // "value" : "fun"
jGenerator.writeStringField("value", "nice"); // "value" : "nice"
jGenerator.writeEndObject(); // }
jGenerator.writeEndArray(); // ]
jGenerator.writeEndObject(); // } end of field_incentives
jGenerator.writeObjectFieldStart("field_community_email"); // "field_community_email" : {
jGenerator.writeFieldName("und"); // "und" :
jGenerator.writeStartArray(); // [
jGenerator.writeStartObject(); // {
jGenerator.writeStringField("value", "[email protected]"); // "value" : "fun"
jGenerator.writeEndObject(); // }
jGenerator.writeEndArray(); // ]
jGenerator.writeEndObject(); // } end of field_community_email
jGenerator.writeEndObject(); // } end of node
jGenerator.writeEndObject(); // }
jGenerator.close();
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
回答by Ilya
Using POJOs
Main
使用 POJO
主要
public class Main {
private Node node;
// getters/setters
Node
节点
public class Node {
private String type;
private FieldIncentives field_incentives;
private FieldIncentives field_community_email;
// getters/setters
FieldIncentives
现场奖励
public class FieldIncentives {
private List<Holder> und;
// getters/setters
Holder
持有者
public class Holder {
private String value;
// getters/setters
Usage
用法
final ObjectMapper o = new ObjectMapper();
final Main m = o.readValue(new File("exampleJson.json"), Main.class);