1

Im trying to take the values from the ArrayList and put in to an JSONObject. I have written the below code but it does only put the last value from arraylist to jsonobject

I am trying to achieve this out put.

{"lstContacts":"array_value"},{"lstContacts":"array_value"},{"lstContacts":"array_value"}

This is my code

ArrayList<String> tokens;
JSONObject contactsObj;
..
...
test.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 try {
 for (int i = 0; i < tokens.size(); i++) {
 contactsObj.put("ContactToken", tokens.get(i));
 }
 } catch (JSONException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 String jsonStr = contactsObj.toString();
 Log.e("CONTACTS", jsonStr); // adds only last array to json object
 }
 });
asked Nov 2, 2015 at 15:47
4
  • 1
    contactsObj needs to be a JSONArray Commented Nov 2, 2015 at 15:48
  • 1
    The output that you posted is not a valid JSONObject. Commented Nov 2, 2015 at 15:51
  • as you can see all the answers provided give you something slightly different. This is a direct result of the fact that we can't tell what you want from the question. You need to figure out exactly what kind of object you're looking for. And by the time you do that, you'll have probably solved the problem on your own anyway Commented Nov 2, 2015 at 15:59
  • git clone github.com/google/gson.git Commented Nov 2, 2015 at 16:05

4 Answers 4

9

Try this:

JSONObject contactsObj = new JSONObject();
JSONArray contactsArray = new JSONArray();
try {
 for (int i = 0; i < tokens.size(); i++) {
 JSONObject contact = new JSONObject();
 contact.put("ContactToken", tokens.get(i));
 contactsArray.put(i, contact);
 }
 contactsObj.put("contacts", contactsArray);
} catch (JSONException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
}
String jsonStr = contactsObj.toString();
Log.e("CONTACTS", jsonStr); // adds only last array to json object

The result jsonStr will look like this:

{ 
 "contacts":[ 
 { 
 "ContactToken":"someToken"
 },
 { 
 "ContactToken":"someToken"
 },
 { 
 "ContactToken":"someToken"
 },
 { 
 "ContactToken":"someToken"
 }
 ]
}
answered Nov 2, 2015 at 15:58
Sign up to request clarification or add additional context in comments.

1 Comment

You should post what the resulting object would look like.
1

You are overriding the object because u are using an JsonObject for an ArrayList, the solution is to use an JsonArray contactObj in your case

JSONArray contactsObj;
 for (int i = 0; i < tokens.size(); i++) {
 contactsObj.put(i, tokens.get(i));
 }
answered Nov 2, 2015 at 15:53

Comments

0
JSONObject contactsObj = new JSONObject();
for (int i = 0; i < tokens.size(); i++) {
 contactsObj.put("lstContacts" + String.valueOf(i), tokens.get(i));
}
// done...
answered Nov 2, 2015 at 15:54

1 Comment

A JSONArray is better but he wants a JSONObject
0
contactsObj.put("ContactTokens", new JSONArray(tokens));

Is probably closest to what you're looking for. You don't even need to loop for this.

This will give you the object

{
 "ContactTokens":["token1","token2","token3"...]
}
answered Nov 2, 2015 at 15:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.