本文介绍了JsonConvert.DeserializeObject和" D"包装在WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过在D包装有默认的WCF服务包JSON响应,我发现一个问题,分析它。

如果我分析是 JsonConvert.DeserializeObject(响应)其中,响应

<$p$p><$c$c>"{\"d\":\"{\"a0b70d2f-7fe4-4aa2-b600-066201eab82d\":\"Thelma\",\"d56d4d4f-6029-40df-a23b-de27617a1e43\":\"Louise\"}\"}"

我GOR错误:

 解析值的意外的字符encoutered后:一。 1号线,9位。
 

如果我改变反应到

<$p$p><$c$c>"{\"a0b70d2f-7fe4-4aa2-b600-066201eab82d\":\"Thelma\",\"d56d4d4f-6029-40df-a23b-de27617a1e43\":\"Louise\"}"

我得到它的工作。

那么如何解析这个D包裹JSON响应从WCF服务?有没有更好的方法来解析JSON?

解决方案

现在我摆脱了D包装与Regex.Replace和修复结构合理JSON响应

  {\GUID \:\a0b70d2f-7fe4-4aa2-b600-066201eab82d \,\名称\:\塞尔玛\}
{\GUID \:\d56d4d4f-6029-40df-A23B-de27617a1e43 \,\名称\:\Lousie \} \}
 

我也做一个类GUID和名称,定义为字符串吧。

然后尝试与反序列化

 名单,其中,MYSTRUCT&GT; O = JsonConvert.DeserializeObject&LT;列表&LT; MYSTRUCT&GT;&GT;(响应);
 

但我得到一个错误

 预计一JsonObjectContract或JsonDictionaryContract类型System.Collections.Generic.List`1 [mynamespace.myStruct]',有'Newtonsoft.Json.Serialization.JsonArrayContract。
 

在哪里招?

By default WCF service wrap JSON response in "d" wrapper and there I found a problem with parsing it.

If I parse with JsonConvert.DeserializeObject(response) where response is

"{\"d\":\"{\"a0b70d2f-7fe4-4aa2-b600-066201eab82d\":\"Thelma\",\"d56d4d4f-6029-40df-a23b-de27617a1e43\":\"Louise\"}\"}"

I gor an Error:

After parsing a value an unexpected character was encoutered: a. Line 1, position 9.

If I change response into

"{\"a0b70d2f-7fe4-4aa2-b600-066201eab82d\":\"Thelma\",\"d56d4d4f-6029-40df-a23b-de27617a1e43\":\"Louise\"}"

I got it working.

So how to parse this "d" wrapped JSON responses from WCF services? Is there any better way to parse JSON?

解决方案

Now I got rid of "d" wrapper with Regex.Replace and fix JSON response with proper structure

{\"Guid\":\"a0b70d2f-7fe4-4aa2-b600-066201eab82d\",\"Name\":\"Thelma\"}
{\"Guid\":\"d56d4d4f-6029-40df-a23b-de27617a1e43\",\"Name\":\"Lousie\"}\"}

I also make a class with Guid and Name, defined as string in it.

Then try to deserialize it with

List<myStruct> o = JsonConvert.DeserializeObject<List<myStruct>>(response);

But i get an error

Expected a JsonObjectContract or JsonDictionaryContract for type 'System.Collections.Generic.List`1[mynamespace.myStruct]', got 'Newtonsoft.Json.Serialization.JsonArrayContract'.

Where is the trick?

这篇关于JsonConvert.DeserializeObject和&QUOT; D&QUOT;包装在WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:58