将ServiceStack.Text反序列化为对象的json始终会转换为字符串,并且在使用引号时表现异常 [英] ServiceStack.Text Deserialize json to object always converts to string and behaves strangely with quotes

查看:123
本文介绍了将ServiceStack.Text反序列化为对象的json始终会转换为字符串,并且在使用引号时表现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做什么:

我有一些json对象,这些对象的值可以是字符串,整数,双精度数或其中任何一个的列表.我正在尝试将这些json字符串反序列化为C#对象,但是由于它们可能具有多种类型,因此我坚持使用通用对象,而不是强类型的替代对象.

I have json objects that have values which could be string, ints, doubles, or lists of any of these. I'm trying to deserialize these json strings into C# objects, but because they could have multiple types, I'm stuck using the generic object, rather than a strongly typed alternative.

我的问题:在T =对象的情况下,似乎ServiceStack.Text.JsonSerializer.DeserializeFromString(jsonString)函数的行为异常.它将始终将事物视为字符串,并且不能使用引号.

My issue: It appears as though the ServiceStack.Text.JsonSerializer.DeserializeFromString(jsonString) function behaves oddly in cases where T = object. It will always treat things as a string, and does not work with quotes.

这是一个例子:

string json1 = "[1]";
string json2 = "[1,2]";
string json3 = "['hello']";
string json4 = "['hello','world']";
string json5 = "[\"hello\"]";
string json6 = "[\"hello\",\"world\"]";
object o1 = JsonSerializer.DeserializeFromString<object>(json1);
object o2 = JsonSerializer.DeserializeFromString<object>(json2);
object o3 = JsonSerializer.DeserializeFromString<object>(json3);
object o4 = JsonSerializer.DeserializeFromString<object>(json4);
object o5 = JsonSerializer.DeserializeFromString<object>(json5);
object o6 = JsonSerializer.DeserializeFromString<object>(json6);

预期的基础对象:

object    type           value
o1        List           [1]
o2        List           [1,2]
o3        List           ['hello']
o4        List           ['hello','world']
o5        List           ["hello"]
o6        List           ["hello","world"]

实际基础对象:

object    type           value
o1        String         "[1]"
o2        String         "[1,2]"
o3        String         "['hello']"
o4        String         "['hello','world']"
o5        String         "["
o6        String         "["

作为参考,使用Newtonsoft.Json的相应代码块将基础对象解释为Netwonsoft.Json.Link.JArray.

For reference, the corresponding code block using Newtonsoft.Json interprets the underlying objects as Netwonsoft.Json.Link.JArray.

就目前而言,我必须确保在json中使用单引号,然后反序列化任何以递归方式提取的字符串,直到正确提取所有内容为止.

As it currently stands, I would have to ensure single quotes are used in the json, and then deserialize any string that was extracted recursively until everything has been properly extracted.

我可以做些什么使它表现出我想要使用ServiceStack.Text的方式吗?

Is there something I can do to have this behave the way I'd like using ServiceStack.Text?

推荐答案

ServiceStack的文本序列化器通过将JSON转换为指定的架构来工作,当您使用对象时,无法提前推断类型,因此为了做到这一点在运行时,ServiceStack的JSON序列化程序需要发出一个专有的 __ type 元数据属性,该属性告诉反序列化程序将其反序列化为什么内容.仅针对 JSON对象文字而不是数组发出,这就是为什么它在这里不起作用的原因.

ServiceStack's text serializers work by converting the JSON into the specified schema, when you use object it's not possible to infer the type ahead of time, so in order to do this at runtime ServiceStack's JSON Serializer needs to emit a proprietary __type metadata property that tells the deserializer what to deserialize it into. This only gets emitted for JSON Object literals and not arrays which is why it doesn't work here.

以下是一些反序列化数组的方法:

Here are some ways to deserialize an array:

string json6 = "[\"hello\",\"world\"]";

var list = json6.FromJson<List<string>>();
list.PrintDump();

var array = json6.FromJson<string[]>();
array.PrintDump();

var arrayObj = json6.FromJson<object[]>();
arrayObj.PrintDump();

这篇关于将ServiceStack.Text反序列化为对象的json始终会转换为字符串,并且在使用引号时表现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆