将JSON字符串反序列化为对象

编程入门 行业动态 更新时间:2024-10-27 13:35:23
本文介绍了将JSON字符串反序列化为对象-Silverlight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

今天下午我花了很多时间试图在字符串中实现JSON的反序列化,起初我使用的是 DataContractJsonSerializer ,因为我的环境是Silverlight,但是它似乎不支持使用开箱即用的字典(在其他许多SO问题中引发).

I've spent a good while this afternoon trying to implement the deserialization of JSON within a string, at first I was using DataContractJsonSerializer as my environment is Silverlight however it does not appear to support using a Dictionary out of the box (Raised in many other SO questions).

作为替代方案,我决定暂时使用JSON.NET(基于上述SO问题的答案),我遇到了以下问题.

As an alternative I decided to use JSON.NET for the time being (Based on the answers to the aforementioned SO questions) and i've hit the following problem.

我想反序列化下面的JSON:

I want to deserialize the JSON below:

{ "disclaimer": "This data is collected from various providers and provided free of charge for informational purposes only, with no guarantee whatsoever of accuracy, validity, availability or fitness for any purpose; use at your own risk. Other than that - have fun, and please share/watch/fork if you think data like this should be free!", "license": "Data collected from various providers with public-facing APIs; copyright may apply; not for resale; no warranties given.", "timestamp": 1334183999, "base": "USD", "rates": { "AED": 3.6732, "AFN": 48.400002, "ALL": 106.669998, } }

并将其放置在以下对象中(在字典中必须为双精度):

and place it within the following object (the double within the dictionary is required):

public class ExchangeData { public string disclaimer { get; set; } public string license { get; set; } public string timestamp { get; set; } public string @base { get; set; } public Dictionary<string, double> rates { get; set; } }

实际上,我最近进行的尝试如下:

My latest attempt at actually getting this to work is below:

StreamReader reader = new StreamReader(args.Result); ExchangeData data = JsonConvert.DeserializeObject<ExchangeData>(reader.ReadToEnd());

但这会导致以下异常:

无法从程序集"System.Core,版本= 3.7.0.0,区域性=中性,PublicKeyToken = 969DB8053D3322AC"中加载类型"System.Dynamic.IDynamicMetaObjectProvider".

根据您所看到的,我的方法是完全错误的,还是我只是犯了一个男生错误(或两者皆有!)

Based on what you can see is my approach completely wrong or am I just making a schoolboy error (or both!)

感谢您的时间!

推荐答案

异常消息本身似乎是一个已知问题,如以下SO问题所提出:

The exception message itself appears to be a known problem as raised in this SO question:

转到JSON.NET 4.0.3的操作中断了我的应用

在使用Nuget安装具有所有必要依赖项的最新软件包之后(我以前是从CodePlex项目中手动下载.DLL的),该代码无需进行其他更改即可工作.

After using Nuget to install the latest package with all necessary dependencies (I manually downloaded the .DLL's from the CodePlex project previously) the code worked with no additional changes.

感谢提供解决方案的用户.

Thank you to the users who provided solutions.

更多推荐

将JSON字符串反序列化为对象

本文发布于:2023-11-17 07:43:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609341.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   序列   对象   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!