[英]convert Jsonobject to JsonArray in android
我是android开发的新手。我想将Jsonobject转换为JsonArray。这是我的代码。
我有一个jsonstring存储在db中。现在我检索该字符串,将它们转换为JSONArray,然后转换为object ..我想从json数组中更新特定的对象字符串,然后再次在db中进行更新。
我的代码是:
JSONArray jsonArray=new JSONArray();
String select_json= customizeAdapter.selectCustomizeEntry_jsonmodified();
try {
JSONObject jsonObject = new JSONObject(select_json);// here response is your json string
JSONObject obj = jsonObject.getJSONObject("fields");
JSONArray array2 = obj.getJSONArray("Feedback");
jsonObject2 = array2.getJSONObject(0);
String Option = jsonObject2.getString("options");
String Option1 = jsonObject2.getString("option1");
String Option2 = jsonObject2.getString("option2");
String Option3 = jsonObject2.getString("option3");
String Option4 = jsonObject2.getString("option4");
edtoption=(EditText) layout.findViewById(R.id.edtoptions);
edtoption.setText(Option);
EditText edtoption1=(EditText) layout.findViewById(R.id.edtoption1);
edtoption1.setText(Option1);
EditText edtoption2=(EditText) layout.findViewById(R.id.edtoption2);
edtoption2.setText(Option2);
EditText edtoption3=(EditText) layout.findViewById(R.id.edtoption3);
edtoption3.setText(Option3);
EditText edtoption4=(EditText) layout.findViewById(R.id.edtoption4);
edtoption4.setText(Option4);
///do something here
Log.e("json Object", jsonObject.toString());
}catch (Exception ex)
{
Log.e("Exception",ex.toString());
}
Button save=(Button)layout.findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("on click","yes");
try {
Log.e("options to edit", jsonObject2.getString("options"));
jsonObject2.remove("options");
jsonObject2.put("options", edtoption.getText());
}catch (Exception er){
Log.e("Exception JSON EDIT",er.toString());
}
}
});
在这里,在“保存单击事件”中,我想更新该选项数据,无论该editText前面写的是什么,都将该对象转换为数组并在db中更新它
请帮我
尝试这个
JSONObject songs= json.getJSONObject("songs");
Iterator x = songs.keys();
JSONArray jsonArray = new JSONArray();
while (x.hasNext()){
String key = (String) x.next();
jsonArray.put(songs.get(key));
}
不管你在获得JsonObject
,只是put
该对象JsonArray
:
JsonArray jsonArray = new JsonArray();
jsonArray.put(jsonObject);
// jsonObject is like { "Name": "Prashant", "RollNo":"10"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.