使用GSON / POJO解析JSON [英] Parse JSON with GSON/POJO
问题描述
响应是一个包含JSON的字符串,我想用GSON / POJO来解析。
我得到错误:
期望的BEGIN_OBJECT,但是在第1行第1列路径$
试图解析时。
$ b $由于:java.lang.IllegalStateException:期望的BEGIN_OBJECT,但是在第1行的路径STRING 1路径$
在com.google.gson.stream.JsonReader.beginObject(JsonReader.java:388 )
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory.java:209)
at com.google.gson.Gson.fromJson(Gson .java:879)
com.google.gson.Gson.fromJson(Gson.java:844)上的
com.google.gson.Gson.fromJson上的
(Gson.java :793)
at com.google.gson.Gson.fromJson(Gson.java:765)
at test.com.example.buddy.myapplicati on.MainActivity $ 8.onResponse(MainActivity.java:192)
//
第192行是 Post组件= gson.fromJson(response,Post.class);
JSON_STRING
它按预期方式工作,我使用POJO类获取值。 String JSON_STRING ={\currentBalance \:{\amount \:0.0,\currencyCode \:\EUR \,\currentBalanceDisplay\\ :true,\overdueAmount \:null,\overdueAmountDisplay\:false,+
\creditAmount \:null,\creditAmountDisplay\: false,\noOfBillsToShow\:3,\recentBills\:[{\period \:\03 2016 \,\amount \:{\ amount \:22.76,+
\currencyCode \:\EUR \},\status \:\PAID \,\ 截止日期\:\ 14-03-2016 \,\sortOrder \:\20160308 \,\periodType \:\MONTHLY \,+
\ invoiceId\:\ 277726719\,\ invoiceDate\:\ 08-03-2016\}]};
如果有人能提供帮助,我将不胜感激。
编辑:我觉得自己像个白痴:)原来我查询的是错误的URL ,一切按预期工作。
来自服务器的字符串响应:
{
currentBalance:{
amount:0.0,
currencyCode:EUR
},
currentBalanceDisplay:true,
overdueAmount:null,
overdueAmountDisplay:false,
creditAmount:null,
creditAmountDisplay:false,
noOfBillsToShow:3,
recentBills:[
{
period:03 2016,
金额:{
金额:12.53,
currencyCode:EUR
},
status:PAID,
dueDate:14-03-2016,
sortOrder:2548264 ,
periodType:MONTHLY,
invoiceId:012345678,
invoiceDate:08-03-2016
}
]
}
排球要求:
private void FetchData(){
StringRequest finalrequest = new StringRequest(Request.Method.POST,F ETCHURL,
新Response.Listener< String>(){
@Override
public void onResponse(String response){
Gson gson = new GSON();
Post组件= gson.fromJson(response,Post.class);
System.out.println(JSON+ component.getRecentBills()。get(0).getInvoiceDate());
//输出:JSON 08-03-2016(成功!)
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
Log.d(ERROR,error finalrequest =>+ error.toString());
$ {
@Override
public String getBodyContentType(){
returnapplication / x-www-form-urlencoded; charset = UTF-8\" ;
}
//这是相关的方法
@Override
public byte [] getBody(){
String httpPostBody =行动= GET_CUST_BILLS&安培; +user =+ CustID;
try {
httpPostBody = httpPostBody + URLEncoder.encode(,UTF-8);
$ b $ catch(UnsupportedEncodingException异常){
Log.e(ERROR,exception,exception);
//如果遇到编码错误,则返回null并且不传递任何POST字符串
return null;
}
Log.d(POSTBODY,httpPostBody.toString());
return httpPostBody.getBytes();
}
};
finalrequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 5,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
TestController.getInstance()。addToRequestQueue(finalrequest,Final);
POJO发布课程:
public class Post {
private CurrentBalanceBean currentBalance;
私有布尔currentBalanceDisplay;
private Object overdueAmount;
private boolean overdueAmountDisplay;
私人对象creditAmount;
私人布尔creditAmountDisplay;
private int noOfBillsToShow;
私人列表< RecentBillsBean> recentBills;
$ b public static PostFromFromData(String str){
return new Gson()。fromJson(str,Post.class);
public static PostFromFromData(String str,String key){
try {
JSONObject jsonObject = new JSONObject(str);
返回新的Gson()。fromJson(jsonObject.getString(str),Post.class);
} catch(JSONException e){
e.printStackTrace();
}
返回null;
}
public static List< Post> arrayPostFromData(String str){
Type listType = new TypeToken< ArrayList< Post>>(){
} .getType();
返回新的Gson()。fromJson(str,listType);
}
public static List< Post> arrayPostFromData(String str,String key){
try {
JSONObject jsonObject = new JSONObject(str);
Type listType = new TypeToken< ArrayList< Post>>(){
} .getType();
返回新的Gson()。fromJson(jsonObject.getString(str),listType);
} catch(JSONException e){
e.printStackTrace();
}
return new ArrayList();
}
public CurrentBalanceBean getCurrentBalance(){
return currentBalance;
}
public void setCurrentBalance(CurrentBalanceBean currentBalance){
this.currentBalance = currentBalance;
}
public boolean isCurrentBalanceDisplay(){
return currentBalanceDisplay;
}
public void setCurrentBalanceDisplay(boolean currentBalanceDisplay){
this.currentBalanceDisplay = currentBalanceDisplay;
}
public Object getOverdueAmount(){
return overdueAmount;
}
public void setOverdueAmount(Object overdueAmount){
this.overdueAmount = overdueAmount;
}
public boolean isOverdueAmountDisplay(){
return overdueAmountDisplay;
}
public void setOverdueAmountDisplay(boolean overdueAmountDisplay){
this.overdueAmountDisplay = overdueAmountDisplay;
}
public Object getCreditAmount(){
return creditAmount;
}
public void setCreditAmount(Object creditAmount){
this.creditAmount = creditAmount;
}
public boolean isCreditAmountDisplay(){
return creditAmountDisplay;
}
public void setCreditAmountDisplay(boolean creditAmountDisplay){
this.creditAmountDisplay = creditAmountDisplay;
}
public int getNoOfBillsToShow(){
return noOfBillsToShow;
}
public void setNoOfBillsToShow(int noOfBillsToShow){
this.noOfBillsToShow = noOfBillsToShow;
}
public List< RecentBillsBean> getRecentBills(){
return recentBills;
}
public void setRecentBills(List< RecentBillsBean> recentBills){
this.recentBills = recentBills;
}
public static class CurrentBalanceBean {
private int amount;
私人字符串currencyCode;
public static CurrentBalanceBean objectFromData(String str){
return new Gson()。fromJson(str,CurrentBalanceBean.class);
$ b $ public static CurrentBalanceBean objectFromData(String str,String key){
try {
JSONObject jsonObject = new JSONObject(str);
返回新的Gson()。fromJson(jsonObject.getString(str),CurrentBalanceBean.class);
} catch(JSONException e){
e.printStackTrace();
}
返回null;
}
public static List< CurrentBalanceBean> arrayCurrentBalanceBeanFromData(String str){
类型listType = new TypeToken< ArrayList< CurrentBalanceBean>>(){
} .getType();
返回新的Gson()。fromJson(str,listType);
}
public static List< CurrentBalanceBean> arrayCurrentBalanceBeanFromData(String str,String key){
尝试{
JSONObject jsonObject = new JSONObject(str);
Type listType = new TypeToken< ArrayList< CurrentBalanceBean>>(){
} .getType();
返回新的Gson()。fromJson(jsonObject.getString(str),listType);
} catch(JSONException e){
e.printStackTrace();
}
return new ArrayList();
}
public int getAmount(){
return amount;
}
public void setAmount(int amount){
this.amount = amount;
}
public String getCurrencyCode(){
return currencyCode;
}
public void setCurrencyCode(String currencyCode){
this.currencyCode = currencyCode;
}
}
public static class RecentBillsBean {
private String period;
/ **
*金额:22.76
* currencyCode:EUR
* /
私人金额金额;
私人字符串状态;
private String dueDate;
private String sortOrder;
private String periodType;
private String invoiceId;
private String invoiceDate;
公共静态RecentBillsBean objectFromData(String str){
返回新的Gson()。fromJson(str,RecentBillsBean.class);
$ b公共静态RecentBillsBean objectFromData(String str,String key){
$ b $尝试{
JSONObject jsonObject = new JSONObject(str);
返回新的Gson()。fromJson(jsonObject.getString(str),RecentBillsBean.class);
} catch(JSONException e){
e.printStackTrace();
}
返回null;
}
public static List< RecentBillsBean> arrayRecentBillsBeanFromData(String str){
Type listType = new TypeToken< ArrayList< RecentBillsBean>>(){
} .getType();
返回新的Gson()。fromJson(str,listType);
}
public static List< RecentBillsBean> arrayRecentBillsBeanFromData(String str,String key){
尝试{
JSONObject jsonObject = new JSONObject(str);
Type listType = new TypeToken< ArrayList< RecentBillsBean>>(){
} .getType();
返回新的Gson()。fromJson(jsonObject.getString(str),listType);
} catch(JSONException e){
e.printStackTrace();
}
return new ArrayList();
}
public String getPeriod(){
return period;
}
public void setPeriod(String period){
this.period = period;
}
public AmountBean getAmount(){
return amount;
}
public void setAmount(AmountBean amount){
this.amount = amount;
}
public String getStatus(){
return status;
}
public void setStatus(String status){
this.status = status;
}
public String getDueDate(){
return dueDate;
}
public void setDueDate(String dueDate){
this.dueDate = dueDate;
}
public String getSortOrder(){
return sortOrder;
}
public void setSortOrder(String sortOrder){
this.sortOrder = sortOrder;
}
public String getPeriodType(){
return periodType;
}
public void setPeriodType(String periodType){
this.periodType = periodType;
}
public String getInvoiceId(){
return invoiceId;
}
public void setInvoiceId(String invoiceId){
this.invoiceId = invoiceId;
}
public String getInvoiceDate(){
return invoiceDate;
}
public void setInvoiceDate(String invoiceDate){
this.invoiceDate = invoiceDate;
}
public static class AmountBean {
private double amount;
私人字符串currencyCode;
$ b $ public static AmountBean objectFromData(String str){
返回新的Gson()。fromJson(str,AmountBean.class);
public static AmountBean objectFromData(String str,String key){
try {
JSONObject jsonObject = new JSONObject(str);
返回新的Gson()。fromJson(jsonObject.getString(str),AmountBean.class);
} catch(JSONException e){
e.printStackTrace();
}
返回null;
}
public static List< AmountBean> arrayAmountBeanFromData(String str){
类型listType = new TypeToken< ArrayList< AmountBean>>(){
} .getType();
返回新的Gson()。fromJson(str,listType);
}
public static List< AmountBean> arrayAmountBeanFromData(String str,String key){
尝试{
JSONObject jsonObject = new JSONObject(str);
Type listType = new TypeToken< ArrayList< AmountBean>>(){
} .getType();
返回新的Gson()。fromJson(jsonObject.getString(str),listType);
} catch(JSONException e){
e.printStackTrace();
}
return new ArrayList();
}
public double getAmount(){
return amount;
}
public void setAmount(double amount){
this.amount = amount;
}
public String getCurrencyCode(){
return currencyCode;
}
public void setCurrencyCode(String currencyCode){
this.currencyCode = currencyCode;
}
}
}
}
2)将Pojo类对象转换为Gson。
$ b 示例: / p>
@Override
public void onResponse(String response){
Gson gson = new Gson ();
发布对象=新帖子();
object.setResponse(response);
字符串gson = gson.fromJson(object,Post.class); //因为你已经覆写了toString()它会返回你设置的响应
System.out.println(gson);
新的Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError错误){
//处理错误
Pojo CLASS
public class Post {
private String resposne;
private int example;
....
public void setResponse(String Response){
this.response = Response;
}
@Override
public String toString(){
return response;
}
}
我希望这对西蒙有帮助。 ThankYou
I am using volley + OkHttp to get some data from a server.
The response is a string containing JSON, which I want to parse using GSON/POJO.
I get the error:
Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
when trying to parse.
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:388)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:209)
at com.google.gson.Gson.fromJson(Gson.java:879)
at com.google.gson.Gson.fromJson(Gson.java:844)
at com.google.gson.Gson.fromJson(Gson.java:793)
at com.google.gson.Gson.fromJson(Gson.java:765)
at test.com.example.buddy.myapplication.MainActivity$8.onResponse(MainActivity.java:192) //
Line 192 is Post component = gson.fromJson(response, Post.class);
On the other hand when I use below JSON_STRING
it works as expected and I get the values using the POJO class.
String JSON_STRING = "{\"currentBalance\":{\"amount\":0.0,\"currencyCode\":\"EUR\"},\"currentBalanceDisplay\":true,\"overdueAmount\":null,\"overdueAmountDisplay\":false," +
"\"creditAmount\":null,\"creditAmountDisplay\":false,\"noOfBillsToShow\":3,\"recentBills\":[{\"period\":\"03 2016\",\"amount\":{\"amount\":22.76," +
"\"currencyCode\":\"EUR\"},\"status\":\"PAID\",\"dueDate\":\"14-03-2016\",\"sortOrder\":\"20160308\",\"periodType\":\"MONTHLY\"," +
"\"invoiceId\":\"277726719\",\"invoiceDate\":\"08-03-2016\"}]}";
I would be grateful if someone could help. Thank you in advance.
EDIT: I feel like a complete idiot :) It turned out I was querying the wrong URL, everything works as expected. Thanks again guys for helping me out.
String response from server:
{
"currentBalance": {
"amount": 0.0,
"currencyCode": "EUR"
},
"currentBalanceDisplay": true,
"overdueAmount": null,
"overdueAmountDisplay": false,
"creditAmount": null,
"creditAmountDisplay": false,
"noOfBillsToShow": 3,
"recentBills": [
{
"period": "03 2016",
"amount": {
"amount": 12.53,
"currencyCode": "EUR"
},
"status": "PAID",
"dueDate": "14-03-2016",
"sortOrder": "2548264",
"periodType": "MONTHLY",
"invoiceId": "012345678",
"invoiceDate": "08-03-2016"
}
]
}
Volley request:
private void FetchData() {
StringRequest finalrequest = new StringRequest(Request.Method.POST, FETCHURL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Gson gson = new Gson();
Post component = gson.fromJson(response, Post.class);
System.out.println("JSON " + component.getRecentBills().get(0).getInvoiceDate());
// Output: JSON 08-03-2016 (success!)
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("ERROR", "error finalrequest => " + error.toString());
}
}
) {
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=utf-8";
}
// this is the relevant method
@Override
public byte[] getBody() {
String httpPostBody = "action=GET_CUST_BILLS&" + "user=" + CustID;
try {
httpPostBody = httpPostBody + URLEncoder.encode("", "UTF-8");
} catch (UnsupportedEncodingException exception) {
Log.e("ERROR", "exception", exception);
// return null and don't pass any POST string if you encounter encoding error
return null;
}
Log.d("POSTBODY ", httpPostBody.toString());
return httpPostBody.getBytes();
}
};
finalrequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 5,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
TestController.getInstance().addToRequestQueue(finalrequest, "Final");
}
POJO Post class:
public class Post {
private CurrentBalanceBean currentBalance;
private boolean currentBalanceDisplay;
private Object overdueAmount;
private boolean overdueAmountDisplay;
private Object creditAmount;
private boolean creditAmountDisplay;
private int noOfBillsToShow;
private List<RecentBillsBean> recentBills;
public static Post objectFromData(String str) {
return new Gson().fromJson(str, Post.class);
}
public static Post objectFromData(String str, String key) {
try {
JSONObject jsonObject = new JSONObject(str);
return new Gson().fromJson(jsonObject.getString(str), Post.class);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
public static List<Post> arrayPostFromData(String str) {
Type listType = new TypeToken<ArrayList<Post>>() {
}.getType();
return new Gson().fromJson(str, listType);
}
public static List<Post> arrayPostFromData(String str, String key) {
try {
JSONObject jsonObject = new JSONObject(str);
Type listType = new TypeToken<ArrayList<Post>>() {
}.getType();
return new Gson().fromJson(jsonObject.getString(str), listType);
} catch (JSONException e) {
e.printStackTrace();
}
return new ArrayList();
}
public CurrentBalanceBean getCurrentBalance() {
return currentBalance;
}
public void setCurrentBalance(CurrentBalanceBean currentBalance) {
this.currentBalance = currentBalance;
}
public boolean isCurrentBalanceDisplay() {
return currentBalanceDisplay;
}
public void setCurrentBalanceDisplay(boolean currentBalanceDisplay) {
this.currentBalanceDisplay = currentBalanceDisplay;
}
public Object getOverdueAmount() {
return overdueAmount;
}
public void setOverdueAmount(Object overdueAmount) {
this.overdueAmount = overdueAmount;
}
public boolean isOverdueAmountDisplay() {
return overdueAmountDisplay;
}
public void setOverdueAmountDisplay(boolean overdueAmountDisplay) {
this.overdueAmountDisplay = overdueAmountDisplay;
}
public Object getCreditAmount() {
return creditAmount;
}
public void setCreditAmount(Object creditAmount) {
this.creditAmount = creditAmount;
}
public boolean isCreditAmountDisplay() {
return creditAmountDisplay;
}
public void setCreditAmountDisplay(boolean creditAmountDisplay) {
this.creditAmountDisplay = creditAmountDisplay;
}
public int getNoOfBillsToShow() {
return noOfBillsToShow;
}
public void setNoOfBillsToShow(int noOfBillsToShow) {
this.noOfBillsToShow = noOfBillsToShow;
}
public List<RecentBillsBean> getRecentBills() {
return recentBills;
}
public void setRecentBills(List<RecentBillsBean> recentBills) {
this.recentBills = recentBills;
}
public static class CurrentBalanceBean {
private int amount;
private String currencyCode;
public static CurrentBalanceBean objectFromData(String str) {
return new Gson().fromJson(str, CurrentBalanceBean.class);
}
public static CurrentBalanceBean objectFromData(String str, String key) {
try {
JSONObject jsonObject = new JSONObject(str);
return new Gson().fromJson(jsonObject.getString(str), CurrentBalanceBean.class);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
public static List<CurrentBalanceBean> arrayCurrentBalanceBeanFromData(String str) {
Type listType = new TypeToken<ArrayList<CurrentBalanceBean>>() {
}.getType();
return new Gson().fromJson(str, listType);
}
public static List<CurrentBalanceBean> arrayCurrentBalanceBeanFromData(String str, String key) {
try {
JSONObject jsonObject = new JSONObject(str);
Type listType = new TypeToken<ArrayList<CurrentBalanceBean>>() {
}.getType();
return new Gson().fromJson(jsonObject.getString(str), listType);
} catch (JSONException e) {
e.printStackTrace();
}
return new ArrayList();
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public String getCurrencyCode() {
return currencyCode;
}
public void setCurrencyCode(String currencyCode) {
this.currencyCode = currencyCode;
}
}
public static class RecentBillsBean {
private String period;
/**
* amount : 22.76
* currencyCode : EUR
*/
private AmountBean amount;
private String status;
private String dueDate;
private String sortOrder;
private String periodType;
private String invoiceId;
private String invoiceDate;
public static RecentBillsBean objectFromData(String str) {
return new Gson().fromJson(str, RecentBillsBean.class);
}
public static RecentBillsBean objectFromData(String str, String key) {
try {
JSONObject jsonObject = new JSONObject(str);
return new Gson().fromJson(jsonObject.getString(str), RecentBillsBean.class);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
public static List<RecentBillsBean> arrayRecentBillsBeanFromData(String str) {
Type listType = new TypeToken<ArrayList<RecentBillsBean>>() {
}.getType();
return new Gson().fromJson(str, listType);
}
public static List<RecentBillsBean> arrayRecentBillsBeanFromData(String str, String key) {
try {
JSONObject jsonObject = new JSONObject(str);
Type listType = new TypeToken<ArrayList<RecentBillsBean>>() {
}.getType();
return new Gson().fromJson(jsonObject.getString(str), listType);
} catch (JSONException e) {
e.printStackTrace();
}
return new ArrayList();
}
public String getPeriod() {
return period;
}
public void setPeriod(String period) {
this.period = period;
}
public AmountBean getAmount() {
return amount;
}
public void setAmount(AmountBean amount) {
this.amount = amount;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getDueDate() {
return dueDate;
}
public void setDueDate(String dueDate) {
this.dueDate = dueDate;
}
public String getSortOrder() {
return sortOrder;
}
public void setSortOrder(String sortOrder) {
this.sortOrder = sortOrder;
}
public String getPeriodType() {
return periodType;
}
public void setPeriodType(String periodType) {
this.periodType = periodType;
}
public String getInvoiceId() {
return invoiceId;
}
public void setInvoiceId(String invoiceId) {
this.invoiceId = invoiceId;
}
public String getInvoiceDate() {
return invoiceDate;
}
public void setInvoiceDate(String invoiceDate) {
this.invoiceDate = invoiceDate;
}
public static class AmountBean {
private double amount;
private String currencyCode;
public static AmountBean objectFromData(String str) {
return new Gson().fromJson(str, AmountBean.class);
}
public static AmountBean objectFromData(String str, String key) {
try {
JSONObject jsonObject = new JSONObject(str);
return new Gson().fromJson(jsonObject.getString(str), AmountBean.class);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
public static List<AmountBean> arrayAmountBeanFromData(String str) {
Type listType = new TypeToken<ArrayList<AmountBean>>() {
}.getType();
return new Gson().fromJson(str, listType);
}
public static List<AmountBean> arrayAmountBeanFromData(String str, String key) {
try {
JSONObject jsonObject = new JSONObject(str);
Type listType = new TypeToken<ArrayList<AmountBean>>() {
}.getType();
return new Gson().fromJson(jsonObject.getString(str), listType);
} catch (JSONException e) {
e.printStackTrace();
}
return new ArrayList();
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public String getCurrencyCode() {
return currencyCode;
}
public void setCurrencyCode(String currencyCode) {
this.currencyCode = currencyCode;
}
}
}
}
1) Store your Json in a Pojo class 2)Convert the Pojo class object to Gson.
Example:
@Override
public void onResponse(String response) {
Gson gson = new Gson();
Post object = new Post();
object.setResponse(response);
String gson = gson.fromJson(object, Post.class);//as you have overrided toString() it will return you the response you have set
System.out.println(gson);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
}
Pojo CLASS
public class Post{
private String resposne;
private int example;
....
public void setResponse(String Response){
this.response = Response;
}
@Override
public String toString() {
return response;
}
}
I hope this was helpful simon. ThankYou
这篇关于使用GSON / POJO解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!