错误分析JSONArray从.NET Web服务返回 [英] Error parsing JSONArray returned from .NET web service

查看:132
本文介绍了错误分析JSONArray从.NET Web服务返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个Web服务,它返回一个JSON字符串如下:

  {checkrecord:[{rollno:ABC2,百分比:40,参加了12遗漏:34}],表1 :[]}

在我的Andr​​oid应用我试图解析字符串到JSONArray,但我不能这样做,因为我得到的logcat以下异常:

  22 11-16:15:57.381:ERROR / log_tag(462):错误分析数据org.json.JSONException:价值与LT; java.lang.String类型的XML不能被转换为JSONArray

如何解决这个问题?

我的Andr​​oid code是如下:

 公共静态JSONArray getJSONfromURL(字符串B)
    {    //初始化
    InputStream为= NULL;
    字符串结果=;
    JSONArray jArray = NULL;    // HTTP POST
    尝试{        HttpClient的HttpClient的=新DefaultHttpClient();
        HttpPost httppost =新HttpPost(URL);
        HTT presponse响应= httpclient.execute(httppost);
        HttpEntity实体= response.getEntity();
        是= entity.getContent();    }赶上(例外五){
        Log.e(log_tag,在HTTP连接错误+ e.toString());
    }    //响应转换为字符串
    尝试{
        读者的BufferedReader =新的BufferedReader(新的InputStreamReader(是,ISO-8859-1),8);
        StringBuilder的SB =新的StringBuilder();
        串线= NULL;
        而((行= reader.readLine())!= NULL){
            sb.append(行+\\ n);
        }
        is.close();
        结果= sb.toString();
    }赶上(例外五)
             {
        Log.e(log_tag,错误转换结果+ e.toString());
     }    //尝试分析字符串到一个JSON数组
    尝试{
            jArray =新JSONArray(结果);
    }
            赶上(JSONException E)
             {
        Log.e(log_tag,错误分析数据+ e.toString());
     }    返回jArray;
       }

这是Web服务code返回的JSON

 公共类服务:System.Web.Services.WebService
{
    [的WebMethod]    公共字符串的getData(字符串rollno)
    {
        JSON字符串;
        尝试
        {            使用(的SqlConnection MyConnection的=新的SqlConnection(@数据源= \\ SQLEX $ P $干燥综合征;初始目录=学生;用户ID = SA;密码= 123))
            {                串选择='+ rollno +\\从哪里checkrecord = rollno \\ SELECT *';
                SqlDataAdapter的大=新SqlDataAdapter的(选择,MyConnection的);
                DataSet的DS =新的DataSet();
                da.Fill(DScheckrecord);
                DataTable的DT =新的DataTable();
                ds.Tables.Add(DT);
                JSON = Newtonsoft.Json.JsonConvert.SerializeObject(DS);            }
        }        赶上(异常前)
        {
            Console.WriteLine(ex.Message);
            返回null;        }        返回JSON;    }


解决方案

您的Web服务不返回纯JSON,它返回JSON作为从XML SOAP Web服务的字符串返回值。事实上,因为你没有发送从客户端的有效SOAP请求也可能是在XML错误消息。

您的网址需要返回原始JSON,与周围没有SOAP信封的XML。谷歌搜索如何写在ASP.NET,WCF或其他质谱技术基于JSON的网络服务,可能是你熟悉的。

修改

如果你想看到你的URL的原始结果,请尝试使用的工具,像 提琴手 以监控HTTP响应。

另外,如果你想从Android的SOAP Web服务调用,检查 ksoap2为Android

I have a web service implemented that returns a JSON String as follows:

 {"checkrecord":[{"rollno":"abc2","percentage":40,"attended":12,"missed":34}],"Table1":[]}

In my Android app I am trying to parse the String to a JSONArray but I am unable to do so because I get the following exception in logcat:

 11-16 22:15:57.381: ERROR/log_tag(462): Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONArray

How to solve this issue?

My Android code is as follows:

public static JSONArray getJSONfromURL(String b)
    {

    //initialize
    InputStream is = null;
    String result = "";
    JSONArray jArray = null;

    //http post
    try{

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    }catch(Exception e){
        Log.e("log_tag", "Error in http connection "+e.toString());
    }

    //convert response to string
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();
    }catch(Exception e)
             {
        Log.e("log_tag", "Error converting result "+e.toString());
     } 

    //try parse the string to a JSON array
    try{
            jArray = new JSONArray(result);
    }
            catch(JSONException e)
             {
        Log.e("log_tag", "Error parsing data "+e.toString());
     }

    return jArray;
       }

This is web service code returning json

     public class Service1 : System.Web.Services.WebService
{
    [WebMethod]

    public String getdata(String rollno)
    {
        String json;
        try
        {

            using (SqlConnection myConnection = new SqlConnection(@"Data Source=\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123"))
            {

                string select = "select * from checkrecord where rollno=\'" + rollno + "\'";
                SqlDataAdapter da = new SqlDataAdapter(select, myConnection);
                DataSet ds = new DataSet();
                da.Fill(ds, "checkrecord");
                DataTable dt = new DataTable();
                ds.Tables.Add(dt);
                json = Newtonsoft.Json.JsonConvert.SerializeObject(ds);

            }
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return null;

        }

        return json;

    }

解决方案

Your web service does not return pure JSON, it returns JSON as a string return value from an XML SOAP web service. In fact, it may be an error message in XML since you are not sending a valid SOAP request from the client.

Your URL needs to return raw JSON, with no SOAP XML Envelope around it. Search google for how to write JSON-based web services in ASP.NET, WCF, or another MS technology that may be familiar to you.

Edit

If you want to see the raw result of your URL, try using a tool like Fiddler to monitor the HTTP response.

Also, if you want to make SOAP web service calls from Android, check out ksoap2 for Android

这篇关于错误分析JSONArray从.NET Web服务返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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