gpt4 book ai didi

java - 如何使用 Jackson 反序列化动态 JSON 字段?

转载 作者:搜寻专家 更新时间:2023-11-01 03:54:10 26 4
gpt4 key购买 nike

如何将具有动态字段(如下面代码中称为故事的对象)的 JSON 对象反序列化为对象数组?

{
stories: {
-IhO1742Lki-Pit0snot: {
sentences: {
-IhO2fyAEn15XUge6HeY: {
userName: "Giulia",
text: "a dude created a new religion called PornDora"
},
-IhO2fyAEn15XUge6HeZ: {
userName: "Will",
text: "but it was kidnapped by a flying burrito copter"
},
-IhO2fyAEn15XUge6HeX: {
userName: "Jasmine",
text: "I went this morning at AngelHack hackaton"
}
}
},
-IhO-gNvUPHpB9fOn-Gm: {
sentences: {
-IhO0PBBnJavU2gfMcVO: {
userName: "Giorgio",
text: "I woke up alone in a dark alley and a yellow cat was starring at me"
},
-IhO11CWL9r6G4Pu8YXx: {
userName: "Will",
text: "the cat runned away when a blurred figure approached me and called my name"
}
}
}
}
}

下面是我用于反序列化的 Java 类:

public class Story {

private String id;
private List sentences;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List getSentences() {
return sentences;
}
public void setSentences(List sentences) {
this.sentences = sentences;
}

}
public class Sentence {

private String id;
private String text;
private String userName;



public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}

}

最佳答案

您是否控制 JSON 序列化的格式?例如,您能否改用以下格式:

{
"stories": [
{
"id": "-IhO1742Lki-Pit0snot",
"sentences": [
{
"id": "-IhO2fyAEn15XUge6HeY",
"text": "a dude created a new religion called PornDora",
"userName": "Giulia"
}
]
)
]
}

如果您控制序列化方面,您可以尝试使用 Polymorphic Type Handling (PTH)。

这将为 Jackson 提供足够的类型信息来反序列化您的对象。

另见 Tatu 的 accompanying blog post了解背景信息。


这是 Jackson 1.x 中 PTH 的代码片段。如果你想使用 Jackson 2.x,你将利用 com.fasterxml packages并且需要稍微更改代码。

// import com.fasterxml.jackson.annotation.JsonTypeInfo;
// import com.fasterxml.jackson.databind.ObjectMapper;
import org.codehaus.jackson.annotate.JsonTypeInfo;
import org.codehaus.jackson.map.ObjectMapper;

public class MyObjectMapperProvider {
final ObjectMapper defaultObjectMapper;

public MyObjectMapperProvider() {
System.out.println("MyObjectMapperProvider()");


this.defaultObjectMapper = new ObjectMapper();
this.defaultObjectMapper.enableDefaultTyping(
ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT
);
// .enableDefaultTyping();
// .enableDefaultTyping(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE);
// .enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT);
// this.defaultObjectMapper.addMixInAnnotations(Bindable.class, MyObjectMapperProvider.MixIn.class);
// this.defaultObjectMapper.addMixInAnnotations(DataModel.WriteOp.class, MyObjectMapperProvider.MixIn.class);
}
}

顺便说一下,您提供的 json 实际上不是有效的 JSON。尝试使用

   {
"stories": {
"-IhO-gNvUPHpB9fOn-Gm": {
"sentences": {
"-IhO0PBBnJavU2gfMcVO": {
"text": "I woke up alone in a dark alley and a yellow cat was starring at me",
"userName": "Giorgio"
},
"-IhO11CWL9r6G4Pu8YXx": {
"text": "the cat runned away when a blurred figure approached me and called my name",
"userName": "Will"
}
}
},
"-IhO1742Lki-Pit0snot": {
"sentences": {
"-IhO2fyAEn15XUge6HeX": {
"text": "I went this morning at AngelHack hackaton",
"userName": "Jasmine"
},
"-IhO2fyAEn15XUge6HeY": {
"text": "a dude created a new religion called PornDora",
"userName": "Giulia"
},
"-IhO2fyAEn15XUge6HeZ": {
"text": "but it was kidnapped by a flying burrito copter",
"userName": "Will"
}
}
}
}
}

您可以通过将其传递到 python -m json.tool 来验证您的 json 是否正确。在 OS X 上,您可以将 json 复制到剪贴板并将其传递给 json.tool,如下所示:

 > pbpaste | python -m json.tool

如果您想尝试一下,这里有一个利用 Jersey 的小例子。使用 curl http://localhost:7654/sandbox/test 来练习一下:

主类.java

import java.net.URI;

import org.glassfish.grizzly.http.server.HttpServer;
// import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MainClass {
private static final Logger LOG = LoggerFactory.getLogger(MainClass.class);
public static final String PATH = "http://localhost:7654/sandbox/";
public static void main(String[] args) {
// create a resource config that scans for JAX-RS resources and providers
// in com.mlbam.monitoring.lsg.data_aggregator package
final ResourceConfig rc = new ResourceConfig()
.packages("com.mlbam.internal")
// .packages("com.mlbam.monitoring.collector.provis")
.register(MyObjectMapperProvider.class)
.register(JacksonFeature.class)
;

// create and start a new instance of grizzly http server
// exposing the Jersey application at BASE_URI
final HttpServer grizzlyServerV1 = GrizzlyHttpServerFactory.createHttpServer(URI.create(PATH), rc);
LOG.info("Jersey app started with WADL available at {}application.wadl", PATH);
try {
Thread.sleep(10_000);
} catch (InterruptedException e) { }
LOG.info("Stopping");
}
}

MyObjectMapperProvider.java:

// import com.fasterxml.jackson.annotation.JsonTypeInfo;
// import com.fasterxml.jackson.databind.ObjectMapper;
import org.codehaus.jackson.annotate.JsonTypeInfo;
import org.codehaus.jackson.map.ObjectMapper;

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

@Provider
public class MyObjectMapperProvider implements ContextResolver<ObjectMapper> {
final ObjectMapper defaultObjectMapper;

public MyObjectMapperProvider() {
System.out.println("MyObjectMapperProvider()");

this.defaultObjectMapper = new ObjectMapper();
this.defaultObjectMapper.enableDefaultTyping(
ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT
);
// .enableDefaultTyping();
// .enableDefaultTyping(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE);
// .enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT);
// this.defaultObjectMapper.addMixInAnnotations(Bindable.class, MyObjectMapperProvider.MixIn.class);
// this.defaultObjectMapper.addMixInAnnotations(DataModel.WriteOp.class, MyObjectMapperProvider.MixIn.class);
}

@Override
public ObjectMapper getContext(Class<?> type) {
System.out.println("MyObjectMapperProvider.getContext(" + type + ")");
return this.defaultObjectMapper;
}


// @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
// public static class MixIn {
// }
}

我的资源.java

import javax.inject.Singleton;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Singleton
@Path("test/")
public class MyResource {
// public Thing[] state = {new Thing("asdf"), new Thing("foo"), new Thing("bar")};
public String[] state = {"asdf", "foo", "bar"};

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response test() {
return Response.ok(state).build();
}
}

依赖关系(如果使用 maven):

<dependencies>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.13</version>
</dependency>
<!-- Jersey Specific Dependencies -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>

关于java - 如何使用 Jackson 反序列化动态 JSON 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13666806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com