I want to convert this string to JSON:
String jsonString =
"{\"id\":1,\"result\":[{\"activityId\":78398,\"employeeId\":131969,\"id\":270035
3,\"revision\":0,\"wageSpecificationId\":0,\"invoiceId\":0,\"hours\":2,\"javaCla
ss\":\"no.tripletex.tcp.model.Hour\",\"projectId\":1164299,\"companyId\":593187,
\"comment\":\"hei\",\"date\":{\"time\":1.3397112e12,\"javaClass\":\"java.sql.Dat
e\"}},{\"activityId\":78399,\"employeeId\":131969,\"id\":2701989,\"revision\":0,
\"wageSpecificationId\":0,\"invoiceId\":0,\"hours\":5,\"javaClass\":\"no.triplet
ex.tcp.model.Hour\",\"projectId\":1166105,\"companyId\":593187,\"comment\":\"hei
du\",\"date\":{\"time\":1.3397112e12,\"javaClass\":\"java.sql.Date\"}}]}";
Where do I initzialize the JSONArray. I'm new to this and I didn't understand
the other threads with the same subject. I understand why it's not running, but
I don't know what to do about it. My code:
Gson gson = new Gson();
String jsonString =
"{\"id\":1,\"result\":[{\"activityId\":78398,\"employeeId\":131969,\"id\":270035
3,\"revision\":0,\"wageSpecificationId\":0,\"invoiceId\":0,\"hours\":2,\"javaCla
ss\":\"no.tripletex.tcp.model.Hour\",\"projectId\":1164299,\"companyId\":593187,
\"comment\":\"hei\",\"date\":{\"time\":1.3397112e12,\"javaClass\":\"java.sql.Dat
e\"}},{\"activityId\":78399,\"employeeId\":131969,\"id\":2701989,\"revision\":0,
\"wageSpecificationId\":0,\"invoiceId\":0,\"hours\":5,\"javaClass\":\"no.triplet
ex.tcp.model.Hour\",\"projectId\":1166105,\"companyId\":593187,\"comment\":\"hei
du\",\"date\":{\"time\":1.3397112e12,\"javaClass\":\"java.sql.Date\"}}]}";
System.out.println(jsonString);
Debug obj = (Debug) gson.fromJson(jsonString, Debug.class);
System.out.println(obj.getResult());
__________________________________________________________
import com.google.gson.annotations.SerializedName;
public class Debug {
@SerializedName("id")
private String id;
@SerializedName("method")
private String method;
@SerializedName("result")
private Result result;
public final String getId() {
return this.id;
}
public final String getMethod() {
return this.method;
}
public final Result getResult() {
return this.result;
}
}
______________________________________________________________________
import com.google.gson.annotations.SerializedName;
public class Result {
@SerializedName("companyId")
private String companyId;
@SerializedName("javaClass")
private String javaClass;
@SerializedName("activityId")
private String activityId;
@SerializedName("projectId")
private String projectId;
@SerializedName("employeeId")
private String employeeId;
@SerializedName("comment")
private String comment;
@SerializedName("revision")
private String revision;
@SerializedName("invoiceId")
private String invoiceId;
@SerializedName("hours")
private String hours;
@SerializedName("wageSpeceficationId")
private String wageSpeceficationId;
@SerializedName("id")
private String id;
@SerializedName("date")
private Date date;
public final String getCompanyId() {
return companyId;
}
public final String getJavaClass() {
return javaClass;
}
public final String getActivityId() {
return activityId;
}
public final String getProjectId() {
return projectId;
}
public final String getEmployeeId() {
return employeeId;
}
public final String getComment() {
return comment;
}
public final String getRevision() {
return revision;
}
public final String getInvoiceId() {
return invoiceId;
}
public final String getHours() {
return hours;
}
public final String getWageSpeceficationId() {
return wageSpeceficationId;
}
public final String getId() {
return id;
}
public final Date getDate() {
return date;
}
}
The output:
Exception in thread "main" com.google.gson.JsonSyntaxException:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at
line 1 column 19
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.Gson.fromJson(Gson.java:791)
at com.google.gson.Gson.fromJson(Gson.java:757)
at com.google.gson.Gson.fromJson(Gson.java:706)
at com.google.gson.Gson.fromJson(Gson.java:678)
at com.example.tutorial.plugins.Test.main(Test.java:24)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was
BEGIN_ARRAY at line 1 column 19
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)
... 7 more
Thanks!
Original issue reported on code.google.com by matiaspe...@gmail.com on 2 Jul 2012 at 1:29
Original issue reported on code.google.com by
matiaspe...@gmail.com
on 2 Jul 2012 at 1:29