English 中文(简体)
如何与Google- Gson和Json.stringfy取得相同的结果
原标题:How to obtain the same result with google-Gson and JSON.stringfy

在我的 Java 代码中, 我有一个类, 成员类型为 < code> Maap< string, String> 。 我用 Google Gson 库将对象转换为 json 字符串和 therversa 。

org网站的JSON图书馆。

问题在于 Javascript has t“ 映射对象 ”, 如果我使用数组来模拟地图, JSON. 的阵列拼写结果与 Map< String, String> 上的 Gson不同 。

我还从图书馆jshshshtable 2.1 (http://www.timdown.co.uk/jshshstable/index.html)中尝试了Hashtable javascript atimple atimitive 2.1 (http://www.timdown.co.uk/jshshstable/index.html),并尝试了一种对JSON的定制方法,但这一方法没有奏效,因为JSON在这些情况下所做的是把根据习惯JSON方法归还的对象捆绑起来。

这就是我得到的:

与Java和Gson:

{
    "var_1": "value_1",
    "var_2": "value_2",
    "map": {
        "name_1": "value_1",
        "name_2": "value_2"
    }
}

与射手和JSON:

{
    "var_1": "value_1",
    "var_2": "value_2",
    "map": [
        ["name_1", "value_1"],
        ["name_2", "value_2"]
    ]
}

是否有办法解决这个问题,从Java和Javamart获得同样的“json字符串”?

提前感谢...

最佳回答

问题在于 射手没有"映射物体"...

是的,它有: < 坚固 > 所有 < / 坚固 > 的 JavaScript 天体是地图。要获取您要查找的东西,请创建此天体图 :

var obj = {
    "var_1": "value_1",
    "var_2": "value_2",
    "map": {
        "name_1": "value_1",
        "name_2": "value_2"
    }
};

...并使用 JSON.streging on it.

var json = JSON.stringify(obj);

var obj {{{{{{{{{{{{}}}} accript < a href=> rel="nofollow">object 初始化器 ,JSON是对象初始化器语法的子集。 : :-) 您可以以其他方式构建该对象 :

var obj = {};
obj.var_1 = "value_1";
obj.var_2 = "value_2";
obj.map = {};
obj.map.name_1 = "value_1";
obj.map.name_2 = "value_2";

...在拨打JSON.stringizing on it.

问题回答

暂无回答




相关问题
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 ...