16
public ArrayList<String> convertJsonToJsonObject(){

    ArrayList<String> mylist = new ArrayList<String> ();

    mylist.add("abc");
    mylist.add("cfd");
    mylist.add("ert");
    mylist.add("fg");
    mylist.add("ujk");
    mylist.add("wer");

    mylist.add("dvb");
    mylist.add("ert");


    System.out.println("JSONObject :: "+(JSONObject)JSONSerializer.toJson(mylist));
}

I am having error at .toJson() method, I am not sure what is the mistake I am making.

1
  • 4
    1. What does the error say ? 2. Which Json library are you using ? Commented Jul 7, 2014 at 17:21

5 Answers 5

23

You can use the GSON library to accomplish this.

 ArrayList<String> mylist = new ArrayList<String> ();
 mylist.add("abc");
 mylist.add("cfd");
 mylist.add("ert");
 mylist.add("fg");
 mylist.add("ujk");
 String json = new Gson().toJson(mylist);

You can refer to the GSON User Guide for more support.

Sign up to request clarification or add additional context in comments.

2 Comments

Be sure to include the GSON library in your project!
i am trying to convert the array into json object.JSONArray jsArray = new JSONArray(mylist); System.out.println("print the array" + jsArray); for (int i = 0; i < mylist.size(); i++) { jsArray.put(mylist.get(i)); } JSONObject obj = new JSONObject(); System.out.println("print he object" + obj);
9
JSONArray jsArray = new JSONArray(mylist);

Or

Gson gson = new Gson();
String jsonArray = gson.toJson(mylist);

Get java-json.jar and Gson.jar here

3 Comments

i got the output in array["acb ", "adg ", "tyy " ].Now i would like to convert the json array to json object.is there any way i can do it?
Hi whatever you are getting it is the json object
Did you understand the concept now may be you were confused because it was not key value pair Read this you will understand it
3
gson.toJson(object);

Gson Google Group

3 Comments

It says,The method toJson(ArrayList<String>) is undefined for the type Object.@Peter Liljenberg
List<String> mylist = new ArrayList<> (); myList.add("hello"); System.out.println(new Gson().toJson(mylist));
Oh, i understand. I give you link to Google Code of this library, you have to download it and add to dependency of your project.
3
ArrayList<Domain> list = new ArrayList<Domain>();
list.add(new Domain());
list.add(new Domain());
list.add(new Domain());
String json = new Gson().toJson(list);

2 Comments

Can U say how to retrive back the values in list?
@ArunJoshla List<Domain> videos = gson.fromJson(json, new TypeToken<List<Domain>>(){}.getType()); , json is string value that initialized last line of answer
2
List<String> bibo = new ArrayList<String>();
bibo.add("obj1");
bibo.add("obj2");
bibo.add("obj3");
String json = new Gson().toJson(bibo);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.