1

I have JSON object like

{
 "projector":"no",
 "video_conference":"no",
 "polycom":"no",
 "lcd":"no",
 "digital_phone":"no",
 "speaker_phone":"no"
}

How do I store the keys in one array and the values in a separate array?

3
  • Everyone will definitely help you once you show what you have tried so far Commented Feb 12, 2013 at 4:57
  • Try Google gson Commented Feb 12, 2013 at 5:02
  • accept some answers also...if you are putting the question. Commented Feb 12, 2013 at 5:52

3 Answers 3

3

Try GSON for converting your java object to json and vice versa.

Refer this link

http://code.google.com/p/google-gson/

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

Comments

1

You may try this.

 String s = " { "projector":"no", "video_conference":"no", "polycom":"no", "lcd":"no", "digital_phone":"no", "speaker_phone":"no" }";

        JSONObject jObject  = new JSONObject(s);
        JSONObject  menu = jObject.getJSONObject("projector");


        Iterator iter = menu.keys();
    String[] keyArr = new String();
    String[] valArr = new String();
    int count = 0;
        while(iter.hasNext()){
                 keyArr[count] = (String)iter.next();

             valArr[count] = menu.getString(key);
           count +=1;

        }

Comments

1

I like Jackson from http://codehaus.org/ for JSON parsing.

String text = "{ \"projector\":\"no\", \"video_conference\":\"no\", \"polycom\":\"no\", \"lcd\":\"no\", \"digital_phone\":\"no\", \"speaker_phone\":\"no\" }";

ObjectMapper mapper = new ObjectMapper();
Map<String,Object> map = mapper.readValue(text, Map.class);
Set<String> k = map.keySet();
Collection<Object> v = map.values();

String[] keys = k.toArray(new String[k.size()]);
String[] values = v.toArray(new String[v.size()]);

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.