我正在开发 Android 3.1 及更高版本的应用程序。
我正在使用适用于 Android 的 Spring Framework 1.0.0.0RC1 和 Google GSon 2.1。
尝试解析 JSON 时出现错误。
这是"http://192.168.1.128/RestServiceImpl.svc/forms/"返回的JSON。
{
"allFormsResult": [
{
"FormId": 1,
"FormName": "Formulario 1"
},
{
"FormId": 2,
"FormName": "Formulario 2"
},
{
"FormId": 3,
"FormName": "Formulario 3"
}
]
}
在这里我做所有事情:
public class FormSpringController
{
public static List<Form> LoadAll()
{
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
String url = "http://192.168.1.128/RestServiceImpl.svc/forms/";
GsonHttpMessageConverter messageConverter = new GsonHttpMessageConverter();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(messageConverter);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setMessageConverters(messageConverters);
ResponseEntity<Form[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Form[].class);
Form[] result= responseEntity.getBody();
return Arrays.asList(result);
}
}
当我尝试解析它时,出现以下错误:
W/System.err(519): Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
你知道我该如何解决吗?
更新
@hotveryspicy 建议我 JSON 存在“问题”。这就是我生成 JSON 响应(C# 代码)的方式:
public class RestServiceImpl : IRestServiceImpl
{
public List<FormContract> allForms()
{
List<FormContract> list = null;
using (var vAdmEntities = new ADMDatabase.ADMEntities())
{
list = new List<FormContract>();
foreach (var form in vAdmEntities.Form)
{
FormContract formC = new FormContract
{
FormName = form.name.Trim(),
FormId = form.formId
};
list.Add(formC);
}
}
return list;
}
}