0

data is selecte all users in database
it will print out:

[com.test.abc.user.domain.User@b22379c, com.test.abc.user.domain.User@364b96e5, com.test.abc.user.domain.User@1c9fb03c, com.test.abc.user.domain.User@37eb41d2]

I want to know how can I get the value in it .

I want to make it to json string like str so that the front end can get it
Please help me !!

@ResponseBody
public String getList() {

    List<User> data = memberObj.getCurrentMembers() ;
    System.out.println(data);

    //data to string

    String str = "{\"data\": [{\"id\": \"1\",\"account\": \"[email protected]\",\"name\": \"sky\",\"nick\": \"abc\"}]}";

    return str;
}

3 Answers 3

1

Use google gson library

List<User> list = new ArrayList<User>();
list.add(new User());
list.add(new User());
String json = new Gson().toJson(list);
Sign up to request clarification or add additional context in comments.

Comments

0

You need to give your User class a toString() method.

class User {

    String name;

    @Override
    public String toString() {
        return name;
    }
}

2 Comments

I think that what the OP is after is some JSON encoding mechanism.
@npinti - It is difficult to tell isn't it.
0

When you printout data with

System.out.println(data)

you just print out the structure, not the objects in it.

Additionaly, you do the same here:

String str = "{\"data\": [{\"id\": \"1\",\"account\": \"[email protected]\",\"name\": \"sky\",\"nick\": \"abc\"}]}";

you just return an object not a value, you have to do this with a for or while loop.

1 Comment

How to do the loop? I have no idea

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.