解析从.NET Web服务返回的JSONArray时出错

时间:2011-11-16 16:54:25

标签: android json parsing

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

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

在我的Android应用程序中,我试图将String解析为JSONArray,但我无法这样做,因为我在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

如何解决这个问题?

我的Android代码如下:

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

这是返回json的Web服务代码

     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;

    }

3 个答案:

答案 0 :(得分:2)

您的结果最初不是JSONArray,而是JSONObject

试试这个:

JSONObject jsonResult = new JSONObject(new String(reader));

JSONArray jArray = jsonResponse.getJSONArray("checkrecord");

答案 1 :(得分:1)

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

您的网址需要返回原始JSON,周围没有SOAP XML Envelope。搜索谷歌如何在ASP.NET,WCF或您可能熟悉的其他MS技术中编写基于JSON的Web服务。

修改

如果您想查看网址的原始结果,请尝试使用 Fiddler 等工具来监控HTTP响应。

另外,如果您想从Android进行SOAP Web服务调用,请查看 ksoap2 for Android

答案 2 :(得分:1)

试试这个

  JSONObject jsonobject = new JSONObject(result);
  JSONArray array = jsonobject.getJSONArray("checkrecord");
      if(array.length()>0){
          int max = array.length();
        for (int i = 0; i < max; i++) {
            JSONObject tmp = array.getJSONObject(i);
            list.add(tmp.getString("rollno"));
            mandatoryFlagList.add(tmp.getString("percentage"));
        }
     }