在android中解析JSON数据时出错

在android中解析JSON数据时出错,android,json,parsing,Android,Json,Parsing,解析JSON字符串时面向JSONException 例外情况: org.json.JSONException: Value anyType of type java.lang.String cannot be converted to JSONArray 代码片段 try { androidHttpTransport.call(Soap_Action1, envelope); SoapObject response = (SoapObject) envelope.getRespon

解析JSON字符串时面向
JSONException

例外情况:

org.json.JSONException: Value anyType of type java.lang.String cannot be converted to JSONArray
代码片段

try {
   androidHttpTransport.call(Soap_Action1, envelope);
   SoapObject response = (SoapObject) envelope.getResponse();
   String resp=response.toString();

   Log.d("resp",response.toString());
   // newwwwww
   try {
       JSONArray jsonArray = new JSONArray(resp);
       for (int i = 0; i < jsonArray.length(); i++) {
           JSONObject c = jsonArray.getJSONObject(i);
           System.out.println(c.getInt("MST_BloodGroupID"));
           System.out.println(c.getString("BloodGroup_Name"));
       }
   } catch (JSONException e) {
       e.printStackTrace();
   }
}
试试看{
调用(Soap_Action1,信封);
SoapObject响应=(SoapObject)信封.getResponse();
String resp=response.toString();
Log.d(“resp”,response.toString());
//新世界
试一试{
JSONArray JSONArray=新JSONArray(resp);
for(int i=0;i
response.toString()如下所示:

anyType{schema=anyType{element=anyType{complexType=anyType{choice=anyType{element=anyType{complexType=anyType{sequence=anyType{element=anyType{}; 元素=任何类型{};};};};};};};};};};};}; diffgram=anyType{DocumentElement=anyType{Table=anyType{MST\u BloodGroupID=1; BloodGroup_Name=A+;};Table=anyType{MST_BloodGroupID=2; BloodGroup_Name=A-;};Table=anyType{MST_BloodGroupID=3; BloodGroup_Name=B+;};Table=anyType{MST_BloodGroupID=4; BloodGroup_Name=B-;};Table=anyType{MST_BloodGroupID=5; BloodGroup_Name=AB+;};Table=anyType{MST_BloodGroupID=6; BloodGroup_Name=AB-;};Table=anyType{MST_BloodGroupID=7; BloodGroup_Name=O+;};Table=anyType{MST_BloodGroupID=8; 血型_Name=O-;};};};}


私有类GetCategories扩展异步任务{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Fetching..");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        ServiceHandler jsonParser = new ServiceHandler();
        String json = jsonParser.makeServiceCall(URL_CATEGORIES, ServiceHandler.GET);

        Log.e("Response: ", "> " + json);

        if (json != null) {
            try {
                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {
                    JSONArray categories = jsonObj
                            .getJSONArray("categories");                        

                    for (int i = 0; i < categories.length(); i++) {
                        JSONObject catObj = (JSONObject) categories.get(i);
                        System.out.println(catObj.getInt("id"));
                        System.out.println(catObj.getString("name"));
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }

        return null;
    }
@覆盖
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(MainActivity.this);
pDialog.setMessage(“获取…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
ServiceHandler jsonParser=新ServiceHandler();
字符串json=jsonParser.makeServiceCall(URL\u CATEGORIES,ServiceHandler.GET);
Log.e(“响应:”,“>”+json);
if(json!=null){
试一试{
JSONObject jsonObj=新的JSONObject(json);
if(jsonObj!=null){
JSONArray categories=jsonObj
.getJSONArray(“类别”);
对于(int i=0;i
您的响应.toString()未返回有效的JSON数据。检查字符串是否为有效的JSON的快速方法是将其插入此站点: 如果它有效,您可以切换到查看器选项卡并可视化您的json,以确保您的代码正确地检查了与括号和花括号相关的JSONArray和JSONObject,我在解析json时遇到的大多数错误都源于我误读了我的数据集。如果它无效,网站会大声向您喊叫,就像它一样用你的数据

我建议您使用GSON库来创建JSON数据。将其导入项目后,您可以像这样使用它:

Gson gson = new Gson();
SoapObject response = (SoapObject) envelope.getResponse();
String resp = gson.toJson(response);

除此之外,您从字符串数据创建JSON对象和数组的方法似乎是正确的。

您无法将字符串转换为JSONArray,因为字符串不是数组,而是JSONObject


尝试将字符串转换为JSONObject,并使用它的键从JSONObject获取数组。

post响应的输出。toString()看起来像是Web服务将“空”字符串返回为
“anyType”
。截至目前,它不是错误中提到的有效JSON数组。请检查您的Web服务逻辑或其调用参数。响应如下:我已发布response.toString()的输出