预期BEGIN_ARRAY,但是带有三个元素的数组的BEGIN_OBJECT [英] Expected BEGIN_ARRAY but was BEGIN_OBJECT with an array of three elements

查看:94
本文介绍了预期BEGIN_ARRAY,但是带有三个元素的数组的BEGIN_OBJECT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Android 3.1及以上版本的应用程序。



我为Android和Google GSon 2.1使用Spring Framework 1.0.0.0RC1。

>

当我试图解析JSON时出现错误。



这是 JSON strong>返回http://192.168.1.128/RestServiceImpl.svc/forms/

 <$ c $ 
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();

返回Arrays.asList(result);
}
}

当我尝试解析它时,出现以下错误:

  W / System.err(519):引起:java.lang.IllegalStateException:期望的BEGIN_ARRAY,但在行BEGIN_OBJECT 1列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(vAdmEntities.Form中的var表单)
{
FormContract formC = new FormContract
{
FormName = form.name.Trim(),
FormId = form.formId
};
list.Add(formC);
}
}

返回列表;
}
}


解决方案

as你的字符串以 {开头,这意味着它是一个Object(json概念),并且你认为它是一个数组,这是错误的。


$ b

编辑:

  {data:
{
allFormsResult:[
{
FormId:1,
FormName:Formulario 1
},
{
FormId :2,
FormName:Formulario 2
},
{
FormId:3,
FormName:Formulario 3




code
$ b $ p
$ b

一个对象数据,然后继续解析。这是Json的一个问题,当它获得第一个元素作为数组 [


I'm developing an Android 3.1 and above application.

I'm using Spring Framework 1.0.0.0RC1 for Android, and Google GSon 2.1.

I'm getting an error when I'm trying to parsing JSON.

This is JSON returned by "http://192.168.1.128/RestServiceImpl.svc/forms/".

{
    "allFormsResult": [
        {
            "FormId": 1,
            "FormName": "Formulario 1"
        },
        {
            "FormId": 2,
            "FormName": "Formulario 2"
        },
        {
            "FormId": 3,
            "FormName": "Formulario 3"
        }
    ]
}

Here I do everything:

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);
    }
}

When I try to parse it I get the following error:

W/System.err(519): Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

Do you know how can I fix it?

UPDATE
@hotveryspicy has suggested me there is a "problem" with JSON. This is how I'm generating JSON response (C# code):

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;
    }
}

解决方案

as your string starts with "{", which means it is an Object(json concept) and it seems that your considering it as an array, which is wrong.

EDITED:

{"data":
   {
    "allFormsResult": [
        {
            "FormId": 1,
            "FormName": "Formulario 1"
        },
        {
            "FormId": 2,
            "FormName": "Formulario 2"
        },
        {
            "FormId": 3,
            "FormName": "Formulario 3"
        }
    ]
  }
}

just append a object "data", and then continue with you parsing. It is a problem of Json when it got first element as an Array "[".

这篇关于预期BEGIN_ARRAY,但是带有三个元素的数组的BEGIN_OBJECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆