首页 新闻 会员 周边

json转成Dictionary

0
悬赏园豆:100 [已解决问题] 解决于 2018-01-15 11:21

如下一串字符串,根据 logID、sign、IP、时间、方法、类型、消息作为key,转成 Dictionary<string, object>

{

"logID":"7",

"sign":"9914df154c1b0f2843849c6b07eead8c",

"IP":"127.0.0.1",

"时间":"2018/1/12 11:06:43",

"方法":"SapDataService.CommonService.GetBusinessData",

"类型":"响应",

"消息":"{"d":{"results":[{"__metadata": {"type":"jkh","uri":"666)"},"DocEntry":2859,"DocNum":1819,"CardCode":"V010394","CardName":"XXXX有限公司","NumAtCard":"46","Comments":"666","PurchaseDelivery1":{"__deferred":{"uri":"fdgdf"}}}]}}"

}

问题补充:

string centos = "{\"logID\":\"7\",\"sign\":\"9914df154c1b0f2843849c6b07eead8c\",\"IP\":\"127.0.0.1\",\"时间\":\"2018/1/12 11:06:43\",\"方法\":\"SapDataService.CommonService.GetBusinessData\",\"类型\":\"响应\",\"消息\":\"{\"d\":{\"results\":[{\"__metadata\": {\"type\":\"jkh\",\"uri\":\"666)\"},\"DocEntry\":2859,\"DocNum\":1819,\"CardCode\":\"V010394\",\"CardName\":\"XXXX有限公司\",\"NumAtCard\":\"46\",\"Comments\":\"666\",\"PurchaseDelivery1\":{\"__deferred\":{\"uri\":\"fdgdf\"}}}]}}\"}";

 

使用下面这个方法报错

  Dictionary<string, object> dicR = StringToDictionary<string, object>(centos);

public static Dictionary<TKey, TValue> StringToDictionary<TKey, TValue>(string jsonStr)
{
if (string.IsNullOrEmpty(jsonStr))
return new Dictionary<TKey, TValue>();

Dictionary<TKey, TValue> jsonDict = JsonConvert.DeserializeObject<Dictionary<TKey, TValue>>(jsonStr);

return jsonDict;

}

母鸡轰的主页 母鸡轰 | 初学一级 | 园豆:74
提问于:2018-01-15 10:11
< >
分享
最佳答案
1

y要把所有字段连接起来当KEY.然后原对象做value?

收获园豆:80
吴瑞祥 | 高人七级 |园豆:29449 | 2018-01-15 10:16

我最后要的数据是  Dictionary<string, object>

logID=7

sign=9914df154c1b0f2843849c6b07eead8c

ip=127.0.0.1

时间=2018/1/12 11:06:43

方法=SapDataService.CommonService.GetBusinessData

类型=响应

消息={"d":{"results":[{"__metadata": {"type":"jkh","uri":"666)"},"DocEntry":2859,"DocNum":1819,"CardCode":"V010394","CardName":"XXXX有限公司","NumAtCard":"46","Comments":"666","PurchaseDelivery1":{"__deferred":{"uri":"fdgdf"}}}]}}

母鸡轰 | 园豆:74 (初学一级) | 2018-01-15 10:20

如果【消息】内不是json数据格式的话,是转换成功的,目前问题就是这样子

母鸡轰 | 园豆:74 (初学一级) | 2018-01-15 10:22

@母鸡轰: 反序列化成动态类呢?

吴瑞祥 | 园豆:29449 (高人七级) | 2018-01-15 10:33

@吴瑞祥: 试过了还是不行

母鸡轰 | 园豆:74 (初学一级) | 2018-01-15 10:34

@母鸡轰:问题在你的json是错误的.

---------------------------
Microsoft Visual Studio
---------------------------
将 JSON 粘贴为类

剪贴板中的内容不是有效的 JSON 实例。请修复该问题,然后重试:
After parsing a value an unexpected character was encountered: d. Path '消息', line 15, position 8.
---------------------------
确定
---------------------------

吴瑞祥 | 园豆:29449 (高人七级) | 2018-01-15 10:45

@吴瑞祥: 

我最后要的数据是  Dictionary<string, object>

logID=7

sign=9914df154c1b0f2843849c6b07eead8c

ip=127.0.0.1

时间=2018/1/12 11:06:43

方法=SapDataService.CommonService.GetBusinessData

类型=响应

消息={"d":{"results":[{"__metadata": {"type":"jkh","uri":"666)"},"DocEntry":2859,"DocNum":1819,"CardCode":"V010394","CardName":"XXXX有限公司","NumAtCard":"46","Comments":"666","PurchaseDelivery1":{"__deferred":{"uri":"fdgdf"}}}]}}

母鸡轰 | 园豆:74 (初学一级) | 2018-01-15 10:50

@吴瑞祥: 不管value内是不是json

母鸡轰 | 园豆:74 (初学一级) | 2018-01-15 10:50

@母鸡轰: ,\"消息\":\"{\"d\":{  这个地方的\":\"{\"引号是有问题的

吴瑞祥 | 园豆:29449 (高人七级) | 2018-01-15 10:51

@母鸡轰: 这个地方会把字符串中断掉.后面的双引号就已经是字符串结束了.所以是错误的.

这个字符串应该是被拼接出来的.生成这个json字符串的时候.用json序列化一下.

吴瑞祥 | 园豆:29449 (高人七级) | 2018-01-15 10:53

@吴瑞祥: 好的,感谢了

母鸡轰 | 园豆:74 (初学一级) | 2018-01-15 11:06
其他回答(2)
0

是否用 Microsoft JScript 里面的,抱什么错,看exception看堆栈三。

收获园豆:10
花飘水流兮 | 园豆:13560 (专家六级) | 2018-01-15 10:28

未处理Newtonsoft.Json.JsonReaderException
HResult=-2146233088
Message=After parsing a value an unexpected character was encountered: d. Path '消息', line 1, position 169.
Source=Newtonsoft.Json
LineNumber=1
LinePosition=169
Path=消息
StackTrace:
在 Newtonsoft.Json.JsonTextReader.ParsePostValue()
在 Newtonsoft.Json.JsonTextReader.ReadInternal()
在 Newtonsoft.Json.JsonTextReader.Read()
在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateDictionary(IDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, JsonProperty containerProperty, String id)
在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
在 Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
在 Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
在 Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
在 Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
在 Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
在 Soholife.Common.Json.ConvertToJson.StringToDictionary[TKey,TValue](String jsonStr)
在 ConsoleApplication3.Program.Test1() 位置 f:\MyProject\Logger\ConsoleApplication3\Program.cs:行号 36
在 ConsoleApplication3.Program.Main(String[] args) 位置 f:\MyProject\Logger\ConsoleApplication3\Program.cs:行号 20
在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:

支持(0) 反对(0) 母鸡轰 | 园豆:74 (初学一级) | 2018-01-15 10:33

具体代码在 问题补充 那里

支持(0) 反对(0) 母鸡轰 | 园豆:74 (初学一级) | 2018-01-15 10:34

@母鸡轰: 这个用Microsoft JScript 里面的来解析成Dictionary。

支持(0) 反对(0) 花飘水流兮 | 园豆:13560 (专家六级) | 2018-01-15 10:42

@花飘水流兮: 请问有具体的代码没

支持(0) 反对(0) 母鸡轰 | 园豆:74 (初学一级) | 2018-01-15 10:45

@母鸡轰: https://www.cnblogs.com/caofangsheng/p/5687994.html

支持(0) 反对(0) 花飘水流兮 | 园豆:13560 (专家六级) | 2018-01-15 11:11

@母鸡轰: 一个类就是一个Dictionary,Object 你自己调试时可以看到类型,如果是个类继续Dictionary,如果是集合你直接转换为集合类型即可。

支持(0) 反对(0) 花飘水流兮 | 园豆:13560 (专家六级) | 2018-01-15 11:13
0

这个json对应的类型并不是Dictionary<string, object>

收获园豆:10
dudu | 园豆:30943 (高人七级) | 2018-01-15 10:41

装成 Dictionary<string, stirng>,或者Dictionary<string, object> 都可以

支持(0) 反对(0) 母鸡轰 | 园豆:74 (初学一级) | 2018-01-15 10:46

上面我的问答有误。由于json序列化时不包含类型信息,所以不能在反序列化时使用object(string除外)

支持(0) 反对(0) dudu | 园豆:30943 (高人七级) | 2018-01-15 10:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册