0

I'm trying to get just the values of a JSONArray that i can store to a ArrayList or an Array. My Code is this at the moment:

  JSONArray params = (JSONArray) res.get("params");
  for (int j = 1; j <= params.size(); j++){

             Object chatter = params.get(j);
             String chatterName = chatter.toString();
             System.out.println("ChatterName: "+chatterName);
             int index = 2;
             listModel.add(index, chatterName);  
             index++;
  }

My problem is that i get the keys too:

ChatterName: Steve
ChatterName: Blubb
ChatterName: 2
ChatterName: 3
ChatterName: Joey
ChatterName: 4
ChatterName: Chris

This is the JSON looks like:

    Input Stream(Response vom Server): {"statuscode":"200","sequence":1382,"response":"sendWho","params":["1","Steve","Blubb","2","3","Joey","4","Chris"]}
6
  • do you mean you are also getting keys in your listModel? Your question in not clear,explain more. Commented May 25, 2015 at 18:38
  • oh, i'm sorry for that. Yes, I want get all the values from the json (in this case the names of the chatters) and put them into a Jlist. And my problem is, that there are also the keys like "2", "3" etc. Hope my problem is more clear now. Commented May 25, 2015 at 18:43
  • As per your JSON "1","2","3" and "4" are also values not keys. "params":["1","Steve","Blubb","2","3","Joey","4","Chris"] Commented May 25, 2015 at 18:45
  • Oh, yes, you're right. Oh my god, I didn't see this. Then I have to check, why. Thank you, RE350! Commented May 25, 2015 at 18:48
  • ok, thats's weird. because I just iterate though an ArrayList and add every single element to the JSONArray. I've no idea where the numbers come from... Commented May 25, 2015 at 18:51

3 Answers 3

1

If you mean that you want to convert it into an ArrayList, then do the following:

JSONArray params = (JSONArray) res.get("params");
ArrayList<String> data = new ArrayList<>(params.size());

for (int j = 0; j < data.size(); j++) {
    data.add(params.get(j).toString());
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thx for your answer. My problem is, that i get values like 1,2 etc althoug i didn't add them to the array and I've no idea why
0

Your JSONArray is not well created because your keys are stored as values. Take a look at:

  "params":["1","Steve","Blubb","2","3","Joey","4","Chris"]

You should have built your array like this

JSONArray list = new JSONArray();
JSONObject obj = new JSONObject();
obj.put("1","Steve");
list.add(obj);
...
JSONObject main = new JSONObject();
main.put("params",list);

if you want to retrieve your values

 ArrayList<String> arr = new ArrayList<String>();
 JSONArray list = (JSONArray) main.get("params");
 for(int i=0;i<list.size();i++)
 {
      arr.add((String)((JSONObject)list.get(i)).get(i));  //because the key is the index in your case
 } 
  //arr contains your values
  //you can also convert them to an array by:
  String[] myarr =new String[arr.size()];
  myarr = arr.toArray(myarr);

2 Comments

good idea. But if i do that something like this happens: {"statuscode":"200","sequence":1167,"response":"sendWho","params":[{"1":"1"},{"2":"Daniel"}]}
@n00bst3r Is there a problem with this ? this is exactly what I expect
0

Ok, I solved the problem. It wasn't a "json-simple" issue, but a logical one. I'm building a server-client chat application and before entering a name, which I'm asked for by the server, I put an id as a key to the client. But I forgot to delete this key and because of this I get the keys too.

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.