Unescape JSON字符串

时间:2011-10-30 09:49:15

标签: json gson

我有一个.NET服务报告回转的JSON字符串

[
"{\"uid\":3298534883331,\"id\":\"foo\",\"name\":\"Foo Bar\",\"address\":{\"uid\":3298534883332,\"city\":\"Shanghai\",\"street\":\"Ke Yuan Street\"}}",
"{\"uid\":3298534883333,\"id\":\"foo\",\"name\":\"Foo Bar\",\"address\":{\"uid\":3298534883334,\"city\":\"Shanghai\",\"street\":\"Ke Yuan Street\"}}"
]

从Java方面来说,我使用的是GSON,并希望获得未转义的JSON

[
{"uid":0,"id":"foo","name":"Foo Bar","address":{"uid":0,"city":"Shanghai","street":"Ke Yuan Street"}},
{"uid":0,"id":"foo","name":"Foo Bar","address":{"uid":0,"city":"Shanghai","street":"Ke Yuan Street"}}
]

unescape JSON字符串是否有任何标准程序?

这是.NET部分

    [OperationContract]
    [WebGet(
        UriTemplate = "{Database}/{ContainerName}?query={tag}",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    string[] Get(string database, string containerName, string tag);

其中使用JSON.net

Java客户端

    String json = webResource.
        queryParam("query", "id = 'foo' AND name = 'Foo Bar'").
        get(String.class);

从Eclipse调试器中我可以看到两个字符串的区别,GSON生成的内容以及返回的内容 您可以为开头和结尾的每个对象注意一个额外的

1 个答案:

答案 0 :(得分:-1)

您的服务器似乎实际上正在返回一个字符串数组:

string[] Get(string database, string containerName, string tag);

这正是客户所得到的:

[
    "string 1",
    "string 2",
    ...
    "string N"
]

您应该返回一个对象数组:

MyClass[] Get(string database, string containerName, string tag);

然后你会得到类似的东西:

[
    {"MyProperty1": "value1_1", ..., "MyPropertyM": "value1_M"},
    {"MyProperty1": "value2_1", ..., "MyPropertyM": "value2_M"},
    ...
    {"MyProperty1": "valueN_1", ..., "MyPropertyM": "valueN_M"},
]

MyClass定义如下:

class MyClass
{
    public string MyProperty1 { get; set; }
    public string MyProperty2 { get; set; }
    ...
    public string MyPropertyM { get; set; }
}