简体   繁体   中英

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},{'NoSQL':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, ie key-value pairs which is just a hashtable eg:

{'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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM