json 中的单个字符串反序列化 c#
Single string in json to deserialze c#
我在 JSON
中有以下字符串
string IDS = "{\"IDS\":\"23,24,25,28\"}"
现在我需要将其转换为 C# 字符串
我试过了
string id = new JavaScriptSerializer().Deserialize<string>(IDS);
我想在字符串中找回这个逗号分隔的字符串,但它抛出错误
No parameterless constructor defined for type of 'System.String
有什么帮助吗?
谢谢
这是因为您的 json 字符串是字典。用这样的东西试试
var result = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(IDS);
var mystring = result["IDS"];
我更喜欢使用 JSON.net 的解决方案:
class Program
{
static void Main(string[] args)
{
string IDS = "{\"IDS\":\"23,24,25,28\"}";
var obj = JsonConvert.DeserializeObject<MyClass>(IDS);
Console.WriteLine(obj.IDS);
Console.ReadLine();
}
}
class MyClass
{
public string IDS { get; set; }
}
发生错误是因为您使用了不正确的类型字符串进行反序列化,它应该是 Matthias Burger 提到的字典。但是你可以使用动态类型,
string json = "{\"IDS\":\"23,24,25,28\"}";
var jobject = new JavaScriptSerializer().Deserialize<dynamic>(json );
string ids = jobject["IDS"]
我在 JSON
中有以下字符串string IDS = "{\"IDS\":\"23,24,25,28\"}"
现在我需要将其转换为 C# 字符串
我试过了
string id = new JavaScriptSerializer().Deserialize<string>(IDS);
我想在字符串中找回这个逗号分隔的字符串,但它抛出错误
No parameterless constructor defined for type of 'System.String
有什么帮助吗?
谢谢
这是因为您的 json 字符串是字典。用这样的东西试试
var result = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(IDS);
var mystring = result["IDS"];
我更喜欢使用 JSON.net 的解决方案:
class Program
{
static void Main(string[] args)
{
string IDS = "{\"IDS\":\"23,24,25,28\"}";
var obj = JsonConvert.DeserializeObject<MyClass>(IDS);
Console.WriteLine(obj.IDS);
Console.ReadLine();
}
}
class MyClass
{
public string IDS { get; set; }
}
发生错误是因为您使用了不正确的类型字符串进行反序列化,它应该是 Matthias Burger 提到的字典。但是你可以使用动态类型,
string json = "{\"IDS\":\"23,24,25,28\"}";
var jobject = new JavaScriptSerializer().Deserialize<dynamic>(json );
string ids = jobject["IDS"]