开发者

json annotation to configure the serialized xml node's name of an object's property

What i need to do is the following. i need using json, to serialize my object to xml as follows:

<employee>
   <name>Name</name>
   <id>the_database_id</id>
<employee>

my java code/bean is as follows:

public class PairPOJO<K,V> implements IPair<K,V> {
    private K first;
    private V second;
...
}

for reasons irrelevant for my problem, firtst/second need to remain with these names...

so the serialization produces

   <employee>
      <first>Name</first>
      <second>the_database_id</second>
   <employee>

I am new to json, is there any bean annotation or 开发者_开发百科any other way letting me to accomplish what i need to do? any site, example, info is highly appreciated.


My code snippet is...

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.xml.XMLSerializer;    
private String serializeItem(Object obj) {
    JSON jsonObject = JSONSerializer.toJSON(obj);
    XMLSerializer xmlSerializer = new XMLSerializer();
    xmlSerializer.setTypeHintsEnabled(true);
    return xmlSerializer.write(json);
}

Eh? You want to use JSON to serialize a java object to XML? That makes no sense. – skaffman 2 hours ago

and

Can you explain why you want to use JSON? It's just a common data format and as such seems like an additional unnecessary translation to go from POJO->JSON->XML. Why not just go POJO->XML?

Is there any better way for me to serialize to xml from complex objects? As i said it is the first time i need to do something similar and got over web that this is the best/better way to do it.

The general idea is that i need to return through REST ws beans.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜