0

我目前正在将 Gson 与 JQWidgets 一起使用。

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

Step2:在jsp的前端,网格行的数据发生了变化,这些变化被推送到一个临时数组中,如下所示。

// 这里创建了一个全局数组,并将网格每一行中所做的数据更改 // 放入数组中

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

随后是 --> var json_string = JSON.stringify(tempArray); 此处,创建 json 字符串时的数据与上面显示的示例数据相同。

Step3:修改后的数据作为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

请帮助并建议如何解决此问题。

感谢和问候牧场

4

2 回答 2

0

它到达我希望在发送时读取它的服务器端代码,我收到以下错误:

您是否在服务器端阅读之前将其转换String为?JSONArray

像这样,

JSONArray array = new JSONArray(json_data);
于 2012-08-10T12:33:54.533 回答
-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": "Berlin", "Country": "Germany" },
{ "CompanyName": "Dubiou", "ContactName": "Ana Trujillo", "ContactTitle": "Sales Manager", "Address ": "Avda. de la Constitucin 2222", "City": "Mxico DF", "Country": "Mexico" }
] }

于 2012-08-10T12:53:18.553 回答