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

标签 android json parsing

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

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

在我的 Android 应用程序中,我试图将字符串解析为 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

如何解决这个问题?

我的安卓代码如下:

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;

    }

最佳答案

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

试试这个:

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

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

关于android - 解析从 .NET Web 服务返回的 JSONArray 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8155510/

相关文章:

python - Django jsonify 模板内的单个模型对象

java - 如何在 MS SQL Server DB 中存储数学公式并使用 JAVA 解释它?

android - iOS View 类似于 Android 上的 Chronometer

java - onPageListener 不工作

php - 如何访问 Json 响应

node.js - 将请求从静态包代理到 Express 后端时,NginX 无法传递 POST 请求正文

java - Scanner#nextDouble 无法解析 5.2 (InputMismatchException)

javascript - 运行 AJAX 调用的 javascript 代码

java - 将一段声音保存为铃声

android - 动态创建drawable资源路径