converting a complex json object to java object through gson
I've a problem with storing a Json object as a java object, I'm not sure what structure to use to store something like this:
'tags':[{'CouchDB':1},{'JSON':1},{'database':1},{'No开发者_如何学PythonSQL':1},{'document_database':1}]
I have tried 2 dimensional arrays, ArrayLists and Hashtables but didn't work, could be down to my poor implementation or I just have it wrong, need help with this ASAP please!
I'm using GSON to convert from the Json String to the Java object, and have other parts working fine, the problem is just having GSON parse this structure properly
- Try using http://jsonlint.com/ to make sure that your JSON is valid (it doesn't seem to be)
- If you change your tags to
{"name":"couchdb"}
, your Java class could look like this:
public class Tag
{
private String name;
...
}
And your container class could have a private List<Tag> tags;
Seems, like your tags are just a bunch of keys with a count (or something along those lines) attached to each one, i.e. key-value pairs which is just a hashtable e.g.:
{'tags':{'CouchDB':1,'JSON':1,'database':1,'NoSQL':1,'document_database':1}}
You should be able to convert the above without any trouble, if you can't I would say you have some sort of configuration issue as opposed to any kind of problem with the format of the data.
精彩评论