【问题标题】:parssingorg.json.JSONException: Value of type java.lang.String cannot be converted to JSONArrayparssingorg.json.JSONException:java.lang.String 类型的值无法转换为 JSONArray
【发布时间】:2012-05-06 11:48:01
【问题描述】:

我使用下面的代码(从 mysql 检索数据),它在 android 模拟器版本 4 中运行没有问题 ,但是当我在 2.1 版中使用相同的代码时,结果出现在 logcat 中

这是错误:

 parssing error org.json.JSONException: A JSONArray text must start with '[' at character 1 of <br />

这是使用 2.2 时的错误 parssingorg.json.JSONException: java.lang.String 类型的值无法转换为 JSONArray

     protected Void doInBackground(Void... params) {
    MealActivity.foodList = new ArrayList<ItemInList>();     

   try
   {
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair",k));
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.2.2/y.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
        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,"utf-8"),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());
         }

        //parsing reesult 

        try{
         Log.e("log_tag", " result before parsing " + result);

         String foodName="";
         int Description=0;

        jArray = new JSONArray(result);
        JSONObject json_data = null;

        for (int i = 0; i < jArray.length(); i++) {
         json_data = jArray.getJSONObject(i);
         if(json_data!=null ) 
          {
             foodName=json_data.getString("Food");
             Description=json_data.getInt("Calories");              
             item.setName(foodName);
             item.setDescription(Description);
             item.setSelected(false);
             MealActivity.foodList.add(item);   
             item=new ItemInList();


                            }
                         }

【问题讨论】:

  • 粘贴给出异常的 json 部分。我猜是json有问题。
  • 你得到的只是一个普通的 jsonobject 而不是一个 json 数组...
  • 好吧,他们可能更新了 json 实现...尝试使用 GSON,看看它是否只适用于那里的 1 或两者都不起作用。
  • @Warpzit 如果它在[] 里面,它就是一个数组,它恰好只有一个元素。
  • 是的,它是数组,有时该方法返回一个元素。谢谢你 Warpzit。

标签: android json


【解决方案1】:

嗯,我现在很确定。您的问题是您试图将 jsonobject 转换为 json 数组。这个问题类似:"A JSONArray text must start with '[' at character 1 of null"。 你应该做的是简单地使用 jsonobject 代替。

【讨论】:

    【解决方案2】:

    以下代码和输入字符串在 2.2 上对我有用。但是,我确实必须清理您的输入字符串,因为它具有一些控制(不可见)字符。你可能想检查一下。

        String data = "[{\"Food\":\"\\u062a\\u0641\\u0627\\u062d \\u0628\\u062f\\u0648\\u0646 \\u0627\\u0644\\u0642\\u0634\\u0631\\u0629\",\"Protein\":\"0.27\",\"Water\":\"86.67\",\"Magnesium\":\"4\",\"Phosphorus\":\"11\",\"Sodium\":\"0\",\"Zinc\":\"0.05\",\"Vit_C\":\"4\",\"Fat_Sat\":\"0\",\"Fat_Mono\":\"0\",\"Vit_B12\":\"0\",\"Calcium\":\"5\",\"Calories\":\"48\",\"Fiber\":\"1\",\"Cholesterole\":\"0\",\"Potassium\":\"90\",\"Sugar_Tot\":\"10.1\",\"Iron\":\"0.07\",\"Folic_Acid\":\"0\",\"carbohydrates\":\"13\",\"Vit_K\":\"0.6\"}]";
        try {
          JSONArray ja = new JSONArray(data);
          Log.d("Sample", "Length:" + ja.length());
          Log.d("Sample", "Data:" + ja.get(0));
        } catch (Exception e) {
          Log.e("Sample", "Error", e);
        }
    

    输出是:

    Length:1
    Data:{"Calories":"48","Calcium":"5","Vit_K":"0.6","Food":"تفاح بدون القشرة","Phosphorus":"11","Potassium":"90","Fat_Mono":"0","Folic_Acid":"0","Cholesterole":"0","Vit_C":"4","Water":"86.67","Vit_B12":"0","Sugar_Tot":"10.1","Sodium":"0","Fat_Sat":"0","Zinc":"0.05","carbohydrates":"13","Magnesium":"4","Iron":"0.07","Fiber":"1","Protein":"0.27"}
    

    【讨论】:

    • 我用了2.2,出现这个错误 parsesingorg.json.JSONException: Value of type java.lang.String cannot be convert to JSONArray.
    • 如何在将字符串转换为数组之前清理字符串
    • 我会说让源服务器知道它发送的控制字符并告诉他们修复它,或者如果控制字符有限,请使用一些正则表达式/简单字符串替换。
    猜你喜欢
    • 2021-07-12
    • 2013-08-24
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    相关资源
    最近更新 更多