gpt4 book ai didi

c# - ServiceStack.Text System.TypeInitializationException 使用 JsonSerializer.DeserializeFromString

转载 作者:太空宇宙 更新时间:2023-11-03 12:59:17 24 4
gpt4 key购买 nike

我正在将 Xamarin.IOS 项目移植到它的统一版本,但我在使用 ServiceStack.Text 包时遇到问题。我已使用 NuGet 将它添加到项目中。

所以我在Xamarin上新建了一个空项目来测试它,问题依旧,所以,这里是测试项目代码:

using UIKit;
using ServiceStack.Text;
using System;

namespace ServiceStackTester
{
public class Test {
public string Field1 { get; set;}
public string Field2 { get; set;}
}

public class Application
{
// This is the main entry point of the application.
static void Main (string[] args)
{
Test t = JsonSerializer.DeserializeFromString<Test>("{Field1:\"A\",Field2:\"B\"}");
Console.WriteLine (t.Field1);

// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main (args, null, "AppDelegate");
}
}
}

JsonSerializer.DeserializeFromString 导致此错误:

    System.TypeInitializationException: An exception was thrown by the type initializer for ServiceStack.Text.Json.JsonReader`1 
---> System.Exception: An exception was thrown by the type initializer for ServiceStack.Text.Json.JsonReader
---> System.Exception: An exception was thrown by the type initializer for ServiceStack.Text.Common.JsReader`1
---> System.Exception: An exception was thrown by the type initializer for ServiceStack.Text.Common.JsWriter
---> System.Exception: An exception was thrown by the type initializer for ServiceStack.Text.JsConfig
---> System.Exception: An exception was thrown by the type initializer for ServiceStack.LicenseUtils
---> System.Exception: Object reference not set to an instance of an object

at ServiceStack.LicenseUtils..cctor () [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at ServiceStack.Text.JsConfig..cctor () [0x00051] in <filename unknown>:0
--- End of inner exception stack trace ---
at ServiceStack.Text.Common.JsWriter..cctor () [0x0005a] in <filename unknown>:0
--- End of inner exception stack trace ---
at ServiceStack.Text.Common.JsReader`1[ServiceStack.Text.Json.JsonTypeSerializer]..cctor () [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at ServiceStack.Text.Json.JsonReader..cctor () [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at ServiceStack.Text.Json.JsonReader`1[ServiceStackTester.Test]..cctor () [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at ServiceStack.Text.JsonSerializer.DeserializeFromString[Test] (System.String value) [0x00012] in <filename unknown>:0
at ServiceStackTester.Application.Main (System.String[] args) [0x00006] in /Users/daniele/Projects/ServiceStackTester/ServiceStackTester/Main.cs:17

这是许可问题还是我遗漏了什么?谢谢,丹妮尔

最佳答案

我认为您缺少字段名称周围的双引号。

"{\"Field1\":\"A\",\"Field2\":\"B\"}"

会成功的。

关于c# - ServiceStack.Text System.TypeInitializationException 使用 JsonSerializer.DeserializeFromString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32883547/

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