Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 反序列化json字符串并提取到模型_C#_Asp.net_Json_.net Core_Json.net - Fatal编程技术网

C# 反序列化json字符串并提取到模型

C# 反序列化json字符串并提取到模型,c#,asp.net,json,.net-core,json.net,C#,Asp.net,Json,.net Core,Json.net,我的API响应是一个json字符串,我需要使用反序列化和IEnumerable将其转换为一个模型 这是我的代码,在调试中,我可以看到返回的json字符串: var responseString = await response.Content.ReadAsStringAsync(); 但是,如果我尝试使用下面的代码将其反序列化为其模型,我会收到一个生成错误 productKeys = await JsonSerializer.DeserializeAsync

我的API响应是一个json字符串,我需要使用反序列化和IEnumerable将其转换为一个模型

这是我的代码,在调试中,我可以看到返回的json字符串:

var responseString = await response.Content.ReadAsStringAsync();
但是,如果我尝试使用下面的代码将其反序列化为其模型,我会收到一个生成错误

productKeys = await JsonSerializer.DeserializeAsync
                     <IEnumerable<ProductKey>>(responseString);
这是我的ProductKey型号

public class ProductKey     
{          
    public int success { get; set; }
    public string resultMessage { get; set; }     
    public List<keyInfo> data { get; set; }      
}      

public class keyInfo     
{      
    public string trialKey { get; set; }    
    public string goodTill { get; set; }    
    public string applyInstructions { get; set; }      
}
公共类ProductKey
{          
公共int成功{get;set;}
公共字符串resultMessage{get;set;}
公共列表数据{get;set;}
}      
公共类keyInfo
{      
公共字符串trialKey{get;set;}
公共字符串goodTill{get;set;}
公共字符串applyInstructions{get;set;}
}
这里是错误。。。我相信这是说我的模型需要容纳一个阵列,但为什么?我没有在JSON中使用数组

JsonSerializationException:无法反序列化当前JSON对象 (例如,{“名称”:“值”})转换为类型 'System.Collections.Generic.IEnumerable'1[coreiWS.Models.ProductKey]' 因为该类型需要一个JSON数组(例如[1,2,3])来反序列化 正确地

是System.Text.Json中的一个方法,它实际上接受一个流,而不是字符串作为参数

您已经有一个字符串,因此应该能够使用
反序列化对象
反序列化该字符串:

如果您使用的是Newtonsoft.Json,则以下内容将反序列化:

// using Newtonsoft.Json
var productKeys = JsonConvert.DeserializeObject<IEnumerable<ProductKey>>(responseString);
问题似乎在于
ProductKey
类中
keyInfo
的定义

如果JSON只包含一个
ProductKey
,则可以省略
IEnumerable

var productKeys = JsonConvert.DeserializeObject<ProductKey>(responseString);
var-productKeys=JsonConvert.DeserializeObject(responseString);

你确定这一行代码中出现构建错误吗?你能分享一个你试图反序列化的json示例吗?Haldo。谢谢。很抱歉,回复很混乱。我通常不使用SO-SO学习。好吧,我已经取得了很大的进步。IENumerable似乎是json数据(数组)列表所需要的.Mine包含一个实体,因此重构代码以删除IENumerable使我达到了需要的位置。我非常感谢您的指导。Thx
public class ProductKey 
{
    public int success { get; set; } 
    public string resultMessage { get; set; } 
    public KeyInfo keyInfo { get; set; } 
}

public class KeyInfo
{
    public string trialKey { get; set; } 
    public string goodTill { get; set; } 
    public string applyInstructions { get; set; }
}
var productKeys = JsonConvert.DeserializeObject<ProductKey>(responseString);