将 JSON 字符串转换为 java 中不同的 java 对象

Convert JSON String to different java Objects in java

我对将 JSON 字符串转换为不同的对象有疑问。

我的要求是我必须获取一个 JSON 字符串,然后我必须从该字符串中获取值并将其保存在 Postgresql 数据库中。

我的JSON字符串是

"followup":[{ "Active" : 1,
  "NextVisit" : "Yes",
  "CreateDate" : "12/17/2014 02:53:43 PM",
  "Designation" : "Yes",
  "ModifiedDate" : "12/17/2014 02:53:43 PM",
  "flag" : "I"
},
"identity":[{ "Active" : 1,
  "Createdate" : "12/17/2014 02:45:12 PM",
  "DistrictID" : "01",
  "HouseNo" : 3456,
  "Code" : 22,
  "ModifiedDate" : "12/17/2014 02:45:12 PM",
  "flag" : "I"
}]

我制作了 2 个实体类,即 followup 和 identity,其变量与 Json String

中的变量相同

我的代码是:

Gson gson = new Gson();
identity hhIden =gson.fromJson(jsonString, identity .class);
followup ls=gson.fromJson(jsonString, followup .class);

我在 netbeans 中遇到以下异常:

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 14
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 14
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
    at com.google.gson.Gson.fromJson(Gson.java:795)
    at com.google.gson.Gson.fromJson(Gson.java:761)
    at com.google.gson.Gson.fromJson(Gson.java:710)
    at com.google.gson.Gson.fromJson(Gson.java:682)
    at server.NewWebService.hello(NewWebService.java:59)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.glassfish.webservices.InstanceResolverImpl.invoke(InstanceResolverImpl.java:144)
    at com.sun.xml.ws.server.InvokerTube.invoke(InvokerTube.java:149)
    at com.sun.xml.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:88)
    at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:1136)
    at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:1050)
    at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:1019)
    at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:877)
    at com.sun.xml.ws.api.pipe.helper.AbstractTubeImpl.process(AbstractTubeImpl.java:136)
    at org.glassfish.webservices.MonitoringPipe.process(MonitoringPipe.java:142)
    at com.sun.xml.ws.api.pipe.helper.PipeAdapter.processRequest(PipeAdapter.java:119)
    at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:1136)
    at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:1050)
    at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:1019)
    at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:877)
    at com.sun.xml.ws.api.pipe.helper.AbstractTubeImpl.process(AbstractTubeImpl.java:136)
    at com.sun.enterprise.security.webservices.CommonServerSecurityPipe.processRequest(CommonServerSecurityPipe.java:209)
    at com.sun.enterprise.security.webservices.CommonServerSecurityPipe.process(CommonServerSecurityPipe.java:141)
    at com.sun.xml.ws.api.pipe.helper.PipeAdapter.processRequest(PipeAdapter.java:119)
    at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:1136)
    at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:1050)
    at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:1019)
    at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:877)
    at com.sun.xml.ws.server.WSEndpointImpl.process(WSEndpointImpl.java:419)
    at com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:868)
    at com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:422)
    at com.sun.xml.ws.transport.http.servlet.ServletAdapter.handle(ServletAdapter.java:169)
    at org.glassfish.webservices.JAXWSServlet.doPost(JAXWSServlet.java:169)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access0(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 14
    at com.google.gson.stream.JsonReader.expect(JsonReader.java:339)
    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
    ... 66 more

请帮我解决一下。 提前致谢。

我的实体 bean 类是:

public class followup {
 private String Active;
 private String NextVisit;
 private String CreateDate;
 private String Designation;
 private String ModifiedDate;
 private String flag;
 public String getActive() {
  return Active;
 }
 public void setActive(String active) {
  Active = active;
 }
 public String getNextVisit() {
  return NextVisit;
 }
 public void setNextVisit(String nextVisit) {
  NextVisit = nextVisit;
 }
 public String getCreateDate() {
  return CreateDate;
 }
 public void setCreateDate(String createDate) {
  CreateDate = createDate;
 }
 public String getDesignation() {
  return Designation;
 }
 public void setDesignation(String designation) {
  Designation = designation;
 }
 public String getModifiedDate() {
  return ModifiedDate;
 }
 public void setModifiedDate(String modifiedDate) {
  ModifiedDate = modifiedDate;
 }
 public String getFlag() {
  return flag;
 }
 public void setFlag(String flag) {
  this.flag = flag;
 }

}

public class identity {
 private String Active;
 private String Createdate;
 private String DistrictID;
 private String HouseNo;
 private String Code;
 private String ModifiedDate;
 private String flag;
 public String getActive() {
  return Active;
 }
 public void setActive(String active) {
  Active = active;
 }
 public String getCreatedate() {
  return Createdate;
 }
 public void setCreatedate(String createdate) {
  Createdate = createdate;
 }
 public String getDistrictID() {
  return DistrictID;
 }
 public void setDistrictID(String districtID) {
  DistrictID = districtID;
 }
 public String getHouseNo() {
  return HouseNo;
 }
 public void setHouseNo(String houseNo) {
  HouseNo = houseNo;
 }
 public String getCode() {
  return Code;
 }
 public void setCode(String code) {
  Code = code;
 }
 public String getModifiedDate() {
  return ModifiedDate;
 }
 public void setModifiedDate(String modifiedDate) {
  ModifiedDate = modifiedDate;
 }
 public String getFlag() {
  return flag;
 }
 public void setFlag(String flag) {
  this.flag = flag;
 }

}

{"followup":{ "Active" : 1,
  "NextVisit" : "Yes",
  "CreateDate" : "12/17/2014 02:53:43 PM",
  "Designation" : "Yes",
  "ModifiedDate" : "12/17/2014 02:53:43 PM",
  "flag" : "I"
},
"identity":{ "Active" : 1,
  "Createdate" : "12/17/2014 02:45:12 PM",
  "DistrictID" : "01",
  "HouseNo" : 3456,
  "Code" : 22,
  "ModifiedDate" : "12/17/2014 02:45:12 PM",
  "flag" : "I"
}}

您还需要一个根实体来一次性解析 followup 和身份。

您的 Json 不正确,因为您必须创建两个对象和对象的属性,并且您创建了一个以数组作为参数的对象。

如果你想检查你的 JSon 去这个网站 Json online build

检查您是否在根上只有类型对象和 string:value 作为属性

{"followup":{ "Active" : 1,
  "NextVisit" : "Yes",
  "CreateDate" : "12/17/2014 02:53:43 PM",
  "Designation" : "Yes",
  "ModifiedDate" : "12/17/2014 02:53:43 PM",
  "flag" : "I"
},
"identity":{ "Active" : 1,
  "Createdate" : "12/17/2014 02:45:12 PM",
  "DistrictID" : "01",
  "HouseNo" : 3456,
  "Code" : 22,
  "ModifiedDate" : "12/17/2014 02:45:12 PM",
  "flag" : "I"
}}

对于您的代码,您只需使用,假设 jsonString 与您看到的字符串相同:

Gson gson = new Gson();
identity hhIden =gson.fromJson(jsonString, identity .class);
followup ls=gson.fromJson(jsonString, followup .class);

我通过为每个对象创建反序列化器解决了这个问题,它将使用反序列化(final JsonElement json,final Type typeOfT,final JsonDeserializationContext context)方法来解析 JSON 字符串和 return 返回 Java 对象。

http://www.javacreed.com/gson-deserialiser-example/

谢谢大家,.....