我是android开发的新手。我只是想从jsonstring中删除对象。 我有jsonstring,我正在将字符串转换为json对象,然后从jsonobject将array..remove对象转换为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":""}]}}
答案 0 :(得分:1)
尝试以下代码来解析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();
}
答案 1 :(得分:1)
只需创建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;
}