0

I actually converted my pojo data into json string this way,

 Gson gson = new GsonBuilder().disableHtmlEscaping().create();
    String json=gson.toJson(user);

I got the json string but that is not the format i actually need, i got

json = {"userID":300,"userName":"asd","password":"s","enabled":1}

So, I want to convert Json string with key-value pair as below ,

{"userID":300,"userName":"asd","password":"s","enabled":1}

into Json string with only value (without key) as below

[300,"asd","s",1]
2
  • 2
    In my opinion, bad practice. You have to define which property has a value and what value have that property. Commented May 14, 2013 at 11:52
  • you should specify if you need an explicit ordering in your list, a dictionary mapping is unordered. Commented May 14, 2013 at 12:10

3 Answers 3

1

So I continue after your string json.

// lets deserialize your json string and get a hashmap
Type collectionType = new TypeToken<HashMap<String, Object>>(){}.getType();
HashMap<String, Object> hm = gson.fromJson(json, collectionType);
String finalJson = gson.toJson(hm.values());
// aand taa-daa!!
System.out.println(finalJson);

now finalJson is [300,"asd","s",1]

Edit: libraries are as following:

import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
Sign up to request clarification or add additional context in comments.

1 Comment

Wouldn't it be simpler gson.toJson(hm.values())? Putting the collection into a list, then converting it to an array... Seems totally unnecessary
0

You could whack the properties of the user into a List<Object> and then JSON that.

This would mean GSON made an JSON array out of the List and you would get what you want.

As this doesn't seem to make much sense as a use case you would have to do a bit of hardcoding - I don't think GSON can do this for you:

final List<Object> props = new LinkedList<>();
props.add(user.getId());
props.add(user.getUserName());
//etc
final String json=gson.toJson(props);

2 Comments

I think it's not a good usage. To do that you have to build an user object first. If you have an array of users, you have to do a loop to add a value for each property (if you get values from a database, for example) and then another loop to extract each value... Too much server overload in my opinion. Anyway, as I said in my answer, this question is a really bad practice. You need to define each property:value for your objects.
@DaGLiMiOuX i agree that it is bad practice, I have said as much in my answer. Your database example - the data is already in this form - there is no reason to create a Collection<User> from the ResultSet only to extract the values again.
0

Could I ask why do you want to do that? If you retrieve that Json without key-value how you will know, for example, that 300 is his id and not his money property?

You can't difference between your properties and I don't really recommend it.

Anyway, the only way I find to do that, is to "break" your string manually, replacing your properties with blank values, like json.replace("\"userID\"", ""); and you should do it for every property.

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.