繁体   English   中英

org.json.JSONException:将jsonstring转换为对象时,反馈类型java.lang.String的值无法转换为JSONArray

[英]org.json.JSONException: Value of Feedback type java.lang.String cannot be converted to JSONArray while converting jsonstring to object

我是android开发的新手。我只想从jsonstring中删除对象。 我有jsonstring,我正在将字符串转换为json对象,然后将数组从jsonobject中删除对象,然后再次转换为jsonstring,将该字符串保存到db中。

但我收到错误org.json.JSONException: Value Feedback of type java.lang.String cannot be converted to JSONArray

这是我的代码:

 public JSONArray convertjsonstringtoarray()
{
    JSONArray jsonArray=new JSONArray();
    String select_json= customizeAdapter.selectCustomizeEntry_jsonmodified();
    Log.e("Json String Select",select_json);
    try {
        JSONObject jsnobject = new JSONObject(select_json);
        jsonArray = new JSONArray("Feedback");

        jsonArray = jsnobject.getJSONArray("Feedback");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject explrObject = jsonArray.getJSONObject(i);
        }

    }catch (Exception j){
        Log.e("Exception JSON", j.toString());
    }
    RemoveJSONArray(jsonArray);
    Log.e("JSOn ARRAY",jsonArray.toString());
    return jsonArray;

}
public static JSONArray RemoveJSONArray( JSONArray jarray) {

    JSONArray Njarray=new JSONArray();
    try{
        for(int i=0;i<jarray.length();i++){
            if(i!=1)
                Njarray.put(jarray.get(i));
        }
    }catch (Exception e){e.printStackTrace();}
    return Njarray;
}

这是我的select_json是:

{"id":0,"name":null,"email":null,"fields":{"Feedback":[{"min":"1","max":"1000","visible":"1","params":"","access":"","registration":"1","type":"text","option1":"","id":1,"option2":"","option3":"","option4":"","fieldcode":"FIELDght66yh","name":"ght66yh","value":"","ordering":1,"tips":"ght66yh","required":"0","published":"1","searchable":"1","options":""},{"min":"1","max":"1000","visible":"1","params":"","access":"","registration":"1","type":"text","option1":"","id":2,"option2":"","option3":"","option4":"","fieldcode":"FIELDbgfhtuu","name":"bgfhtuu","value":"","ordering":2,"tips":"bgfhtuu","required":"0","published":"1","searchable":"1","options":""}]}}

尝试下面的代码来解析json响应。

try {
        JSONObject jsonObject = new JSONObject(response);// here response is your json string

        String id = jsonObject.getString("id");
        /**
         * Same way you can get other string value
         */ 
        JSONObject obj =  jsonObject.getJSONObject("fields");

        JSONArray array2 = obj.getJSONArray("Feedback");

        for (int i = 0; i < array2.length(); i++) {
            JSONObject jsonObject2 = array2.getJSONObject(i);
            ///do something here    
        }   

} catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}

只需创建jsonarray的对象。然后将jsnobject.getJSONArray(“ Feedback”)分配给该对象即可。无需在构造函数中传递“ Feedback”字符串

public JSONArray convertjsonstringtoarray()
{
JSONArray jsonArray=new JSONArray();
String select_json= customizeAdapter.selectCustomizeEntry_jsonmodified();
Log.e("Json String Select",select_json);
try {
    JSONObject jsnobject = new JSONObject(select_json);
    //assume this object "jsnobject" is contain the below json
    //dont know what's this select_json is
    /*"{
    "Feedback": [
        {
            "min": "1",
            "max": "1000",
            "visible": "1",
            "params": "",
            "access": "",
            "registration": "1",
            "type": "text",
            "option1": "",
            "id": 1,
            "option2": "",
            "option3": "",
            "option4": "",
            "fieldcode": "FIELDght66yh",
            "name": "ght66yh",
            "value": "",
            "ordering": 1,
            "tips": "ght66yh",
            "required": "0",
            "published": "1",
            "searchable": "1",
            "options": ""
        }]}*/
    //jsonArray = new JSONArray("Feedback");//your code is problematic here
    //just create the object lik
    jsonArray = new JSONArray();
    //jsonArray = new JSONObject() //dont know..why you doing that

    jsonArray = jsnobject.getJSONArray("Feedback");
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject explrObject = jsonArray.getJSONObject(i);
    }

}catch (Exception j){
    Log.e("Exception JSON", j.toString());
}
RemoveJSONArray(jsonArray);
Log.e("JSOn ARRAY",jsonArray.toString());
return jsonArray;

}
public static JSONArray RemoveJSONArray( JSONArray jarray) {

JSONArray Njarray=new JSONArray();
try{
    for(int i=0;i<jarray.length();i++){
        if(i!=1)
            Njarray.put(jarray.get(i));
    }
}catch (Exception e){e.printStackTrace();}
return Njarray;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM