2013-02-03 104 views
2

假設我有這些JSON字符串:獲取字符串值

string s1 = "{\"foo\":\"wh\"o\"a\"}"; 
string s2 = "{\"foo\":{\"bar\":123}}"; 

我想富的字符串格式的價值,所以從S1的我想"wh\"o\"a"進出S2的我想"{\"bar\":123}" 。但是,如果我這樣做

JsonObject j = JsonObject.Parse(s1); 
string foo = j.Get("foo"); 

富包含S1 "wh""{"從S2。如何使用JsonObject獲取JSON字段的原始字符串值?

回答

2

第一個字符串是不是有效的JSON,它可能應該是:

string s1 = "{\"foo\":\"wh\\\"o\\\"a\"}"; 

JsonObject.Get返回一個轉義JSON字符串,如果你想要一個非轉義的字符串,你需要調用JsonObject.GetUnescaped,如:

string s1 = "{\"foo\":\"wh\\\"o\\\"a\"}"; 
string s2 = "{\"foo\":{\"bar\":123}}"; 

JsonObject.Parse(s1).GetUnescaped("foo").Print(); // wh\"o\"a 
JsonObject.Parse(s2).GetUnescaped("foo").Print(); // {"bar":123}