如何在JavaPOJO类中反序列化json对象?
我有一个简单的JSON语句,它的类型非常符合需要。像这样如何在JavaPOJO类中反序列化json对象?,json,web-services,rest,serialization,jaxb,Json,Web Services,Rest,Serialization,Jaxb,我有一个简单的JSON语句,它的类型非常符合需要。像这样 { actor:{name:"kumar",mbox:"kumar@gmail.com"} verb :"completed" } 或 我使用POJO类映射这个json字符串,POJO类代码如下所示 @JsonProperty("actor") Actor actor; @JsonProperty("verb") Verb objVerb; @JsonProperty("verb") String
{
actor:{name:"kumar",mbox:"kumar@gmail.com"}
verb :"completed"
}
或
我使用POJO类映射这个json字符串,POJO类代码如下所示
@JsonProperty("actor")
Actor actor;
@JsonProperty("verb")
Verb objVerb;
@JsonProperty("verb")
String verb;
public Actor getActor() {
return actor;
}
public void setActor(Actor actor) {
this.actor = actor;
}
public Verb getObjVerb() {
return objVerb;
}
public void setObjVerb(Verb objVerb) {
this.objVerb = objVerb;
}
@JsonIgnore
public String getVerb() {
return verb;
}
@JsonIgnore
public void setVerb(String verb) {
this.verb = verb;
}
public static class Actor {
String objectType;
@JsonProperty("name")
ArrayList<String> listName;
@JsonProperty("name")
String name;
@JsonProperty("mbox")
ArrayList<String> listMbox;
@JsonProperty("mbox")
String mbox;
@JsonProperty("mbox_sha1sum")
ArrayList<String> Listmbox_sha1sum;
@JsonProperty("mbox_sha1sum")
String mbox_sha1sum;
@JsonProperty("openid")
String openid;
@JsonProperty("account")
Account account;
public String getObjectType() {
return objectType;
}
public void setObjectType(String objectType) {
this.objectType = objectType;
}
public ArrayList<String> getListName() {
return listName;
}
public void setListName(ArrayList<String> listName) {
this.listName = listName;
}
@JsonIgnore
public String getName() {
return name;
}
@JsonIgnore
public void setName(String name) {
this.name = name;
}
public ArrayList<String> getListMbox() {
return listMbox;
}
public void setListMbox(ArrayList<String> listMbox) {
this.listMbox = listMbox;
}
@JsonIgnore
public String getMbox() {
return mbox;
}
@JsonIgnore
public void setMbox(String mbox) {
this.mbox = mbox;
}
public ArrayList<String> getListmbox_sha1sum() {
return Listmbox_sha1sum;
}
public void setListmbox_sha1sum(ArrayList<String> listmbox_sha1sum) {
Listmbox_sha1sum = listmbox_sha1sum;
}
@JsonIgnore
public String getMbox_sha1sum() {
return mbox_sha1sum;
}
@JsonIgnore
public void setMbox_sha1sum(String mbox_sha1sum) {
this.mbox_sha1sum = mbox_sha1sum;
}
public String getOpenid() {
return openid;
}
public void setOpenid(String openid) {
this.openid = openid;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public static class Account {
@JsonProperty("homePage")
String homePage;
@JsonProperty("name")
String name;
public String getHomePage() {
return homePage;
}
public void setHomePage(String homePage) {
this.homePage = homePage;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
public static class Verb {
String id;
Map<String,String> display;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Map<String, String> getDisplay() {
return display;
}
public void setDisplay(Map<String, String> display) {
this.display = display;
}
}
@JsonProperty(“参与者”)
演员;
@JsonProperty(“动词”)
动词obj动词;
@JsonProperty(“动词”)
字符串动词;
公共参与者getActor(){
回归演员;
}
公共无效集合参与者(参与者){
这个演员=演员;
}
公共动词getObjVerb(){
返回动词;
}
public void setObjVerb(动词objVerb){
this.objVerb=objVerb;
}
@杰索尼奥雷
公共字符串getVerb(){
返回动词;
}
@杰索尼奥雷
公共void集合动词(字符串动词){
这个动词=动词;
}
公共静态类参与者{
字符串对象类型;
@JsonProperty(“名称”)
ArrayList列表名;
@JsonProperty(“名称”)
字符串名;
@JsonProperty(“mbox”)
ArrayList listMbox;
@JsonProperty(“mbox”)
字符串mbox;
@JsonProperty(“mbox_sha1sum”)
ArrayList Listmbox_sha1sum;
@JsonProperty(“mbox_sha1sum”)
串mbox_sha1sum;
@JsonProperty(“openid”)
字符串openid;
@JsonProperty(“账户”)
账户;
公共字符串getObjectType(){
返回objectType;
}
public void setObjectType(字符串objectType){
this.objectType=objectType;
}
公共ArrayList getListName(){
返回列表名;
}
public void setListName(ArrayList listName){
this.listName=listName;
}
@杰索尼奥雷
公共字符串getName(){
返回名称;
}
@杰索尼奥雷
公共void集合名(字符串名){
this.name=名称;
}
公共数组列表getListMbox(){
返回listMbox;
}
公共无效setListMbox(ArrayList listMbox){
this.listMbox=listMbox;
}
@杰索尼奥雷
公共字符串getMbox(){
返回mbox;
}
@杰索尼奥雷
公共void setMbox(字符串mbox){
this.mbox=mbox;
}
公共阵列列表getListmbox_sha1sum(){
返回列表MBOx_sha1sum;
}
公共无效集合listmbox_sha1sum(ArrayList listmbox_sha1sum){
Listmbox_sha1sum=Listmbox_sha1sum;
}
@杰索尼奥雷
公共字符串getMbox_sha1sum(){
返回mbox_sha1sum;
}
@杰索尼奥雷
公共无效设置mbox_sha1sum(字符串mbox_sha1sum){
this.mbox_sha1sum=mbox_sha1sum;
}
公共字符串getOpenid(){
返回openid;
}
public void setOpenid(字符串openid){
this.openid=openid;
}
公共帐户getAccount(){
返回帐户;
}
公共作废设置帐户(帐户){
这个账户=账户;
}
公共静态类帐户{
@JsonProperty(“主页”)
字符串主页;
@JsonProperty(“名称”)
字符串名;
公共字符串getHomePage(){
返回主页;
}
公共主页(字符串主页){
this.homePage=主页;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
}
}
公共静态类动词{
字符串id;
地图显示;
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
公共地图getDisplay(){
返回显示;
}
公共显示(地图显示){
this.display=显示;
}
}
我正在使用jaxb和jakson。我正在实现webservice来处理json语句
所以我使用bean类来映射json。但是当我使用映射这个json时,它会给出以下异常
org.codehaus.jackson.map.JsonMappingException:名为“mbox”的属性有两个条目
定义适当的bean结构,使其直接映射到bean类尝试只保留
@JsonProperty(“mbox”)ArrayList listMbox代码>字段(不需要@JsonProperty(“mbox”)
字符串mbox;
)
并添加功能。将\u SINGLE\u VALUE作为\u ARRAY=true接受到Jackson对象映射器配置中
因此,在反序列化中,它将能够以数组和单个元素的形式获取数据。您可以使用gson
类cls=gson.fromJson(jsonString,clazz)
在这里,jsonString可以被字符串化为java脚本对象。gson.fromJson方法可以将java脚本键映射到java属性。但是我可以为动词做什么呢。这也是产生异常的原因。什么异常?关于动词:我不知道。您不能更改json的语法/结构吗?这不是一个很好的解决方案,但您可以使用@JsonProperty(“动词”)对象objVerb代码>,并且在反序列化时,它将在一个案例中位于另一个映射中的字符串(您可以执行强制转换)。不要使用诸如“请,帮助我等”之类的词语。相反,要清楚地解释你的问题,并写下你努力解决问题的方式。并接受或张贴您的答案,如果您找到了解决方案。
@JsonProperty("actor")
Actor actor;
@JsonProperty("verb")
Verb objVerb;
@JsonProperty("verb")
String verb;
public Actor getActor() {
return actor;
}
public void setActor(Actor actor) {
this.actor = actor;
}
public Verb getObjVerb() {
return objVerb;
}
public void setObjVerb(Verb objVerb) {
this.objVerb = objVerb;
}
@JsonIgnore
public String getVerb() {
return verb;
}
@JsonIgnore
public void setVerb(String verb) {
this.verb = verb;
}
public static class Actor {
String objectType;
@JsonProperty("name")
ArrayList<String> listName;
@JsonProperty("name")
String name;
@JsonProperty("mbox")
ArrayList<String> listMbox;
@JsonProperty("mbox")
String mbox;
@JsonProperty("mbox_sha1sum")
ArrayList<String> Listmbox_sha1sum;
@JsonProperty("mbox_sha1sum")
String mbox_sha1sum;
@JsonProperty("openid")
String openid;
@JsonProperty("account")
Account account;
public String getObjectType() {
return objectType;
}
public void setObjectType(String objectType) {
this.objectType = objectType;
}
public ArrayList<String> getListName() {
return listName;
}
public void setListName(ArrayList<String> listName) {
this.listName = listName;
}
@JsonIgnore
public String getName() {
return name;
}
@JsonIgnore
public void setName(String name) {
this.name = name;
}
public ArrayList<String> getListMbox() {
return listMbox;
}
public void setListMbox(ArrayList<String> listMbox) {
this.listMbox = listMbox;
}
@JsonIgnore
public String getMbox() {
return mbox;
}
@JsonIgnore
public void setMbox(String mbox) {
this.mbox = mbox;
}
public ArrayList<String> getListmbox_sha1sum() {
return Listmbox_sha1sum;
}
public void setListmbox_sha1sum(ArrayList<String> listmbox_sha1sum) {
Listmbox_sha1sum = listmbox_sha1sum;
}
@JsonIgnore
public String getMbox_sha1sum() {
return mbox_sha1sum;
}
@JsonIgnore
public void setMbox_sha1sum(String mbox_sha1sum) {
this.mbox_sha1sum = mbox_sha1sum;
}
public String getOpenid() {
return openid;
}
public void setOpenid(String openid) {
this.openid = openid;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public static class Account {
@JsonProperty("homePage")
String homePage;
@JsonProperty("name")
String name;
public String getHomePage() {
return homePage;
}
public void setHomePage(String homePage) {
this.homePage = homePage;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
public static class Verb {
String id;
Map<String,String> display;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Map<String, String> getDisplay() {
return display;
}
public void setDisplay(Map<String, String> display) {
this.display = display;
}
}