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"]}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
n00bst3r
  • 637
  • 2
  • 9
  • 17
  • do you mean you are also getting keys in your listModel? Your question in not clear,explain more. – dReAmEr May 25 '15 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. – n00bst3r May 25 '15 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"] – dReAmEr May 25 '15 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! – n00bst3r May 25 '15 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... – n00bst3r May 25 '15 at 18:51
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders May 25 '15 at 19:23

3 Answers3

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());
}
MJSG
  • 1,035
  • 8
  • 12
  • 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 – n00bst3r May 25 '15 at 19:08
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);
  • good idea. But if i do that something like this happens: {"statuscode":"200","sequence":1167,"response":"sendWho","params":[{"1":"1"},{"2":"Daniel"}]} – n00bst3r May 25 '15 at 19:06
  • @n00bst3r Is there a problem with this ? this is exactly what I expect –  May 25 '15 at 20:38
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.

krentm
  • 132
  • 12
n00bst3r
  • 637
  • 2
  • 9
  • 17