2011-10-30 136 views
2

我有一個報告回逃脫JSON字符串UNESCAPE 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一個.NET服務,並希望有轉義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生成和什麼回來 你可以注意一個額外的「每個對象在開始和結束

+0

你是如何顯示的字符串?它真的*逃脫了,還是隻是從調試器中獲得的東西?你如何從你的.NET應用程序返回字符串? –

+0

更新後的模式詳細信息 – user579806

回答

-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; } 
}