jsonarray文本必须以{[my return value}的字符1处的'['开头 [英] a jsonarray text must start with '[' at character 1 of {my returned value}

查看:94
本文介绍了jsonarray文本必须以{[my return value}的字符1处的'['开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是RESTFull WCF服务的新手,从Android应用程序调用它们时甚至还比较新.这是我的WCF服务:

I'm very new to RESTFull WCF Services and even newer to calling them from an Android app. Here's my WCF service:

[ServiceContract]
    public interface IPeople
    {
        [OperationContract]
        void DoWork();

        [WebGet(UriTemplate = "/GetPeople",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json)]
        [OperationContract]
        string GetPeople();
    }

接口的实现:

公共字符串GetPeople() { PeoplesEntities qe =新的PeoplesEntities();

public string GetPeople() { PeoplesEntities qe = new PeoplesEntities();

             var result = from q in qe.tPeople
                          select q;


             int count = result.Count();
             int index = new Random().Next(count);

             tPeople people = result.OrderBy(a=>a.ID).Skip(index).FirstOrDefault();

             // result.First().ToString();


             return people.FirstName + " - " + people.LastName;
     }

这就是我通过Android服务使用它的方式:

and this is how i'm consuming it through an android service:

尝试{

       HttpGet request = new HttpGet(SERVICE_URI + "/GetPeople");
       request.setHeader("Accept", "application/json");
       request.setHeader("Content-type", "application/json");

       DefaultHttpClient httpClient = new DefaultHttpClient();
       HttpResponse response = httpClient.execute(request);

       HttpEntity responseEntity = response.getEntity();

       // Read response data into buffer
       char[] buffer = new char[(int)responseEntity.getContentLength()];
       InputStream stream = responseEntity.getContent();
       InputStreamReader reader = new InputStreamReader(stream);
       reader.read(buffer);
       stream.close();

       JSONArray plates = new JSONArray(new String(buffer));
               return new String(buffer);



   } catch (Exception e) {
       e.printStackTrace();
       return e.toString();
   }
}

我得到的例外是该主题中提到的内容.奇怪的是,异常会返回预期值.我不知道为什么要期待方括号.

The exception I get is what is mentioned in the subject. What's strange is the value tha ti'm expecting is returned in the exception. I have no clue why it's expecting the square bracket.

仅供参考,我使用的大多数代码直接来自在线示例.任何帮助将不胜感激.谢谢.

FYI, most of the code i used is taken directly from online examples. Any help would be greatly appreciated. Thanks.

推荐答案

您正试图从不包含有效JSON数组语法的字符串中创建JSONArray.可以从格式为[item1, item2, item3....]的字符串创建JSONArray,但是您只是在字符串中返回单个项目:FirstName LastName.

You're trying to create a JSONArray from a string that doesn't contain valid JSON array syntax. A JSONArray can be created from a string of the form [item1, item2, item3....] but you're just returning a single item in your string: FirstName LastName.

它后面的那一行仅返回缓冲区,因此JSONArray调用是毫无意义的.您根本不需要JSONArray调用,因为您不需要处理JSON数据.只需删除该行即可.

The line after it just returns the buffer, so the JSONArray call is pointless, anyway. You don't need the JSONArray call at all, since you're not dealing with JSON data. Just remove that line.

这篇关于jsonarray文本必须以{[my return value}的字符1处的'['开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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