English 中文(简体)
Java XStream with HashMap
原标题:Java XStream with HashMap

我想利用XStream将java的 has变成一个json的 has。 我认为,这比似乎更容易。 我期望的是:

Map<String, String> map = new HashMap<String, String>();
map.put("first", "value1");
map.put("second", "value2");

加入

{ first  :  value1 ,  second  :  value2  }

The closes I have converts it into a series of arrays.

XStream xstream = new XStream(new JettisonMappedXmlDriver() {
    public HierarchicalStreamWriter createWriter(Writer writer) {
        return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE);
    }
});

xstream.toXML(map);

which 加入s

[["first", "value1"], ["second", "value2"]]

我认为,把一个 j瓦的 has变成一个son,应该直截了当。 我失踪了吗?

问题回答

事实是,XStream首先是为了向XML发射和未婚 Java物体,而JSON只是经过深思熟虑的,它当然得到的支持最少。

技术性问题是,由于XStream必须支持XML和JSON格式,JSON地图的代表性受到影响,因为在XML中不存在代表类似地图结构的本土方法。

You can try to use the "official" json lib for java from json.org.

呼吁:

JSONObject jsobj = new JSONObject(map);
String strJson = jsobj.toString();

我在皈依 j时也有类似问题。 我解决这一问题的办法是,在将档案(就我而言是数据库)投放之前,先把已经编排的插图格式交给联合森。 我迄今提出的最有效率的进程是,在我的班子内建立Json功能,以便像String那样工作。

例:

Converts the objects data output string into Json format

public JsonObject toJson()
   {

       JsonObject temp = new JsonObject();
       temp.addProperty(tagName,floatData);
       return temp;
    }

因此,你在绘制你的地图时,也开展了类似的进程。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签