gpt4 book ai didi

c# - 无法在 Windows 通用应用程序中使用 JavaScriptSerializer

转载 作者:行者123 更新时间:2023-11-30 23:08:57 26 4
gpt4 key购买 nike

我制作了从 Visual Studio 2017 C# 控制台应用程序中的 Web 服务获取 JSON 数据的原型(prototype),将 JSON 数据反序列化为三个字符串。然后我将代码移到 Windows 10 通用应用程序中。

反序列化代码不会在 UWP 程序中编译,因为类型 JavaScriptSerializer 不可用。当我的 Windows 10 解决方案打开时,我无法将 System.Web.Extensions 添加到系统(引用/程序集/框架),从 VS 2017 收到此消息“机器上未找到框架程序集”。

这是 JSON 字符串:

{  
   "Snippet":"\"Special counsel ...\"",
   "SnippetDate":"9/9/2017 12:00:00 AM",
   "SnippetSource":"The Washington Post"
}

以下是在控制台项目而非 Windows 10 项目上编译和运行的代码片段。

using System.Web.Script.Serialization;
.
.
.

public class NewsSnippet
{
public string Snippet { get; set; }
public string SnippetDate { get; set; }
public string SnippetSource { get; set; }
}

.
.
.

var serializer = new JavaScriptSerializer();
var deserializedResult = serializer.Deserialize<NewsSnippet>(jsonString);

在通用 Windows 应用程序中反序列化 JSON 字符串有哪些替代方法?

最佳答案

您应该能够添加 Newtonsoft's Json.net通过Nuget

var deserializedResult = JsonConvert.DeserializeObject<NewsSnippet>(jsonString);
var source = deserializedResult.SnippetSource;

关于c# - 无法在 Windows 通用应用程序中使用 JavaScriptSerializer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46146254/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com