简体   繁体   English

JSON.NET将System.Object属性的类型自动更改为System.String

[英]JSON.NET changes type of a System.Object property automatically to System.String

I have a class named Arg . 我有一个名为Arg的类。 The Property ArgValue is of the Type System.Object . 属性ArgValue的类型为System.Object

public class Arg
{
    public string ArgName { get; set; }
    public object ArgValue { get; set; }
}

If I want to deserialize a JSON-string like this: 如果我想反序列化JSON字符串,如下所示:

string json = @"{""ArgName"":""arg"",""ArgValue"":""/something/more/than/a/string/""}";

var jsonSerializerSettings = new JsonSerializerSettings() { };

Arg arg = JsonConvert.DeserializeObject<Arg>(json, jsonSerializerSettings);

I get the Arg object with its values like this: 我得到的Arg对象的值如下所示:

  • ArgName is of type string -> arg ArgName的类型为string -> arg
  • ArgValue is of type object {string} -> /something/more/than/a/string/ ArgValue的类型为object {string} -> /something/more/than/a/string/

I do not want that JSON.NET is converting it into System.String . 我不希望JSON.NET将其转换为System.String If the property is of type System.Object it should not convert it into a string. 如果属性的类型为System.Object ,则不应将其转换为字符串。

It would be better it leaves it untouched and the value would be of type: JToken (or JValue ). 最好使其保持不变,并且值的类型为: JToken (或JValue )。

Is there a JsonSerializerSettings option to configure this behaviour? 是否有JsonSerializerSettings选项来配置此行为?

Additional information: 附加信息:

The string value in the property ArgValue is "more than a string" and I need to convert it later by myself. 属性ArgValue的字符串值是“多于一个字符串”,我稍后需要自己进行转换。 JsonConverter cannot be used here, because i do not know the concrete type of the object at this point. JsonConverter不能在这里使用,因为此时我不知道对象的具体类型。 later i know and need to use JToken.ToObject<MySpecialType>() . 稍后我知道并且需要使用JToken.ToObject<MySpecialType>() (Than it gets correctly converted by a JsonConverter ). (比起JsonConverter正确地进行了转换)。

As I stated in the comment, the json string you want to deserialize will be deserialized into an object of a class with two properties, both of type string. 正如我在评论中所述,要反序列化的json字符串将反序列化为具有两个属性的类的对象,这两个属性均为string类型。 This is because when you specify "ArgName" : "arg" the two quotes between arg mark a value of type string. 这是因为当你指定"ArgName" : "arg"之间的两个引号arg标记字符串类型的值。 This is also the case of ArgValue which is serialized also as a string. ArgValue也是如此, ArgValue也被序列化为字符串。

By doing this line of code: 通过执行以下代码行:

Arg arg = JsonConvert.DeserializeObject<Arg>(json, jsonSerializerSettings);

You receive an object ( arg ) of type Arg which is exactly as a classical object of type Arg . 收到的对象( arg类型的) Arg ,其是完全一样的类型的古典对象Arg It is the equivalent of doing: 这等效于:

Arg arg = new Arg{ ArgName = "arg", ArgValue = "/something/more/than/a/string/" };

The string assignation of property ArgValue which expects an object is the same in the case of deserialize, it is no conversion (or better said cast ) involved, it is only boxing . 在反序列化的情况下,期望对象的属性ArgValue的字符串分配是相同的,它不涉及转换(或更佳地说是cast ),仅是装箱

For later conversion you can use 对于以后的转换,您可以使用

Generics 泛型

public class Arg<T>
{
    public string ArgName { get; set; }
    public T ArgValue { get; set; }
}

then you can pass target type as follows: 那么您可以按以下方式传递目标类型:

JsonConvert.DeserializeObject<Arg<Qualifiedname>>(somestring);

or 要么

public Arg<T> Deserialize<T>(string somestring){
  return JsonConvert.DeserializeObject<Arg<T>>(somestring);
}

Leave it as JToken 保留为JToken

public class Arg
{
    public string ArgName { get; set; }
    public JToken ArgValue { get; set; }
}

Deserialize it later with JsonConvert 稍后使用JsonConvert其反序列化

public class Arg
{
    public string ArgName { get; set; }
    public string ArgValue { get; set; }
}

or simply pass it whole as string until you know what type is going to be parsed.. 或者只是将其整体作为字符串传递,直到您知道将要解析的类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型“ System.String”强制转换为类型“ System.Object” - Unable to cast the type 'System.String' to type 'System.Object' 无法将“ System.Object []”转换为类型“ System.String []” - Cannot convert 'System.Object[]' to the type 'System.String[]' LINQ to Entities无法识别方法&#39;System.Object Parse(System.Type,System.String)&#39; - LINQ to Entities does not recognize the method 'System.Object Parse(System.Type, System.String)' 多维数组转换运行时错误---&gt;无法将类型为&#39;System.Object [,]&#39;的对象转换为&#39;System.String类型 - Multidimensional Array Casting Runtime Error ---> unable to cast object of type 'System.Object[,]' to type 'System.String 无法将类型为&#39;System.Object []&#39;的对象强制转换为&#39;System.String []&#39; - Unable to cast object of type 'System.Object[]' to type 'System.String[]' .NET Class属性作为对象,例如System.String - .NET Class property as object, such as System.String System.Object[] 不能转换为 System.String[] - System.Object[] can't be converted to System.String[] Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' - Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' Asp.net WebService 无法将“System.Int32”类型的 object 转换为 ty...neric.IDictionary`2[System.String,System.Object]' - Asp.net WebService Cannot convert object of type 'System.Int32' to ty…neric.IDictionary`2[System.String,System.Object]' 方法未找到:&#39;System.String System.String.Format(System.IFormatProvider, System.String, System.Object) - Method not found: 'System.String System.String.Format(System.IFormatProvider, System.String, System.Object)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM