在C#中将字符串转换为对象 [英] Convert string to object in c#
本文介绍了在C#中将字符串转换为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,形式为
"{value: "some"}"
(通过序列化对象获得,但属性名称上不带引号)或
"{"value": "some"}"
I have a string in the form of
"{value: "some"}"
(Obtained by serializing object but without quotes on the property name) OR
"{"value": "some"}"
我希望将其转换为对象(类似于新的{value = "some"}
)
而不是JObject {"value" = "some"}
I wish to convert it a object (Similar to new {value = "some"}
)
And not JObject {"value" = "some"}
有帮助吗?
推荐答案
检查此处获取有关使用Json.NET反序列化匿名类型的信息.
Check here for info on deserializing anonymous types using Json.NET.
var definition = new { Name = "" };
string json1 = @"{'Name':'James'}";
var customer1 = JsonConvert.DeserializeAnonymousType(json1, definition);
Console.WriteLine(customer1.Name);
// James
string json2 = @"{'Name':'Mike'}";
var customer2 = JsonConvert.DeserializeAnonymousType(json2, definition);
Console.WriteLine(customer2.Name);
// Mike
这篇关于在C#中将字符串转换为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文