java - 在android中将Jsonobject转换为JsonArray

标签 java android json

我是 android 开发新手..我想将 Jsonobject 转换为 JsonArray.. 这是我的代码..

我有一个 jsonstring 存储在 db 中..现在我检索该字符串,将它们转换为 JSONArray,然后转换为对象.. 我想从 json 数组更新该特定对象字符串,然后再次在数据库中更新它..

我的代码是:

  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());
            }
        }
    });

在这里,在保存单击事件中,我想更新该编辑文本前面写入的选项数据 将该对象转换为数组并在数据库中更新它

请帮助我

最佳答案

无论您在 JsonObject 中获得什么,只需将该对象放入JsonArray 中即可:

JsonArray jsonArray = new JsonArray();
jsonArray.put(jsonObject); 
// jsonObject is like { "Name": "Prashant", "RollNo":"10"} 

关于java - 在android中将Jsonobject转换为JsonArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30323794/

相关文章:

Java 类型 i18n (java)

Java 字符串第 n 位切分

java - 更改 Google Maps API 的 "My location"按钮的位置

Android:如何画圆角线?

javascript - Json 请求适用于 safari,但不适用于 chrome/firefox

java - Java 调用 apache 的 xml-rpc 客户端后获取 XML 和 JSON 结果时出错 - 意外的非空白字符数据

java - 除非整个盒子都填满,否则不要进入下一个 Activity

php - JSON.stringify - 如何在 Symfony2 中使用它

java - 客户端发送的请求在语法上不正确 - JSON POST 正文中的类型复杂

java - Android opengl绘图文字