Vidhi
Vidhi

Reputation: 409

convert Jsonobject to JsonArray in android

I am new in android development..I want to convert Jsonobject to JsonArray.. Here is my code..

I have one jsonstring which is stored in db..now I retrieve that string , convert them into JSONArray and then object.. I want to update that particular object string from json array and just update it in db again..

My code is :

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

Here In save click event, I want to update that option data whatever written in front of that editText convert that object to array and update it in db

Please help me

Upvotes: 3

Views: 5372

Answers (2)

Chandan Yadav
Chandan Yadav

Reputation: 144

Whatever you get in JsonObject, just put that object in JsonArray:

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

Upvotes: 4

Quick learner
Quick learner

Reputation: 11457

Try this

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

Upvotes: 2

Related Questions