2012-08-10 180 views
0

我目前正在使用Gson和JQWidgets。com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:預期的BEGIN_ARRAY,但是STRING

第1步:使用gson使用它的api將JSON數據以格式發送到網格,作爲JQWidgets支持的行。

[ 
    { "CompanyName": "Alfreds Futterkiste", "ContactName": "Maria Anders", "ContactTitle": "Sales Representative", "Address": "Obere Str. 57", "City": "Berlin", "Country": "Germany" }, 
    { "CompanyName": "Ana Trujillo Emparedados y helados", "ContactName": "Ana Trujillo", "ContactTitle": "Owner", "Address": "Avda. de la Constitucin 2222", "City": "Mxico D.F.", "Country": "Mexico" }, 
    { "CompanyName": "Antonio Moreno Taquera", "ContactName": "Antonio Moreno", "ContactTitle": "Owner", "Address": "Mataderos 2312", "City": "Mxico D.F.", "Country": "Mexico" } 
] 

步驟2:在對JSP網格行的數據前端被改變, 這些變化在一個臨時數組如下推動。

//這裏創建了全局陣列,和數據變更 //網格的每行中進行被放入陣列

var tempArray = new Array(); 
    { 
    var data = $('#jqxgridreq').jqxGrid('getrowdata', args.rowindex); 
    var arr = $.makeArray(data); 
    tempArray.push(data);     
    } 

後來又跟着是 - >無功json_string = JSON .stringify(tempArray); 這裏,數據與創建json字符串時的上述示例數據相同。

第三步:更改後的數據發送到服務器AJAX調用,如下

[ 
    { "CompanyName": "IOK", "ContactName": "Maria Anders", "ContactTitle": "Sales Head", "Address": "Obere Str. 57", "City": "Berlin", "Country": "Germany" },      
    { "CompanyName": "Dubiou", "ContactName": "Ana Trujillo", "ContactTitle": "Sales Manager", "Address": "Avda. de la Constitucin 2222", "City": "Mxico D.F.", "Country": "Mexico" }      
] 

到達服務器端的代碼,我想讀它,因爲它被送往,我也得到了以下錯誤:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING 

請幫助並建議如何解決此問題。

感謝和問候 Ranchs

回答

0

It reaches the server side code where i wish to read it as it was sent and i get the below Error:

你轉換StringJSONArray閱讀之前在服務器端?

這樣,

JSONArray array = new JSONArray(json_data); 
-1

GSON節選要與POJO映射的關鍵,像

public class Test { 
    private List<Type> attr; 
    } 

的東西,這ATTR必須存在於JSON {」 ATTR「{」CompanyName「:」IOK「,」ContactName「:」Maria Anders「,」ContactTitle「:」Sales Head「,」Address「:」Obere Str.57「,」City「 ,「國家」:「德國」},
{「CompanyName」:「Dubiou」,「ContactName」:「Ana Trujillo」,「ContactTitle」:「銷售經理」,「地址」:「Avda。德拉Constitucin 2222「,」城市「:」墨西哥D.F.「,」國家「:」墨西哥「}
]}

相關問題