jags kk
jags kk

Reputation: 1

Convert JSON String to different java Objects in java

I have doubt in converting JSON String to Different Objects.

My requirement is I have to take a JSON string and from that string I have to get and persist the values in Postgresql database.

My JSON String is

"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"
}]

I made 2 entity classes i.e. followup and identity with same variables as that of in Json String

and my code is:

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

and I am getting following exception in 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$1.invoke(InstanceResolverImpl.java:144)
    at com.sun.xml.ws.server.InvokerTube$2.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$2.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$9.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.access$100(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

Please help me to solve it. Thanks in advance.

My entity bean classes are :

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;
	}

}

Upvotes: 0

Views: 574

Answers (3)

jags kk
jags kk

Reputation: 1

I solved the issue by creating Deserializer for each object which will use deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) method to parse the JSON string and return back the Java Object.

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

Thanks all,,,,,.....

Upvotes: 0

ppascualv
ppascualv

Reputation: 1137

Your Json is not correct, because you have to create two Objects and attributes to your objects and you create one objects with arrays as parameters.

If you want to check your JSon go to this website Json online build

Check if on the root you have only type object and string:value as attribute

{"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"
}}

And with your code you only have to use, assuming jsonString is the same string as yo seen up:

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

Upvotes: 1

Alessandro Santini
Alessandro Santini

Reputation: 2003

{"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"
}}

You also need a root entity to parse followup and identity in one go.

Upvotes: 0

Related Questions