3

I'm new to java so this is a bit confusing

I want to get json formatted string

The result I want is

{ "user": [ "name", "lamis" ] }

What I'm currently doing is this :

JSONObject json = new JSONObject();         
json.put("name", "Lamis");
System.out.println(json.toString());

And I'm getting this result

{"name":"Lamis"}

I tried this but it didnt work json.put("user", json.put("name", "Lamis"));

2
  • 2
    Nothing in your code mentions user so why do you expect it in the output? Commented May 22, 2012 at 14:32
  • I know , I dont know how to place it right.. but it gave weird results Commented May 22, 2012 at 14:33

3 Answers 3

14

Try this:

JSONObject json = new JSONObject();         
json.put("user", new JSONArray(new Object[] { "name", "Lamis"} ));
System.out.println(json.toString());

However the "wrong" result you showed would be a more natural mapping of "there's a user with the name "lamis" than the "correct" result.

Why do you think the "correct" result is better?

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

4 Comments

Hmm I want this to add "age" too ..am I messing things up?
What's wrong with {"name":"Lamis", "age":23}? That's what a JSON object should look like and for that you're on the right path with your original code.
Thanks anyways, this is exactly what I wanted
Yes I'm realizing that now, but still it was good to know how to do it, because it took sometime of me
8

Another way of doing it is to use a JSONArray for presenting a list

   JSONArray arr = new JSONArray();
   arr.put("name");
   arr.put("lamis");

   JSONObject json = new JSONObject();
   json.put("user", arr);

   System.out.println(json);   //{ "user": [ "name", "lamis" ] }

Comments

1

Probably what you are after is different than what you think you need;

You should have a separate 'User' object to hold all properties like name, age etc etc. And then that object should have a method giving you the Json representation of the object...

You can check the code below;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

public class User {
    String  name;
    Integer age;

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public JSONObject toJson() {
        try {
            JSONObject json = new JSONObject();
            json.put("name", name);
            json.put("age", age);
            return json;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        User lamis = new User("lamis", 23);
        System.out.println(lamis.toJson());
    }
}

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.