Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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#_Json_Api_Json.net - Fatal编程技术网

C# JSON按特定字段转换项目

C# JSON按特定字段转换项目,c#,json,api,json.net,C#,Json,Api,Json.net,我想用Newtonsoft转换 我只想查看类型字段,然后转换为类型的正确项,因此我没有在所有项中都包含所有字段 我希望能够按类型确定类,然后反序列化。有一个名为TypeIdentifier的原始POCO,它只有一个单独标识类型的类型,然后使用原始POCO 用于将任何JSON转换为C#类(POCO),并使用反序列化 public class HakernewsTypeIdentifier { [JsonProperty("type")] public string Type { g

我想用Newtonsoft转换

我只想查看类型字段,然后转换为类型的正确项,因此我没有在所有项中都包含所有字段


我希望能够按类型确定类,然后反序列化。

有一个名为TypeIdentifier的原始POCO,它只有一个单独标识类型的类型,然后使用原始POCO

用于将任何JSON转换为C#类(POCO),并使用反序列化

public class HakernewsTypeIdentifier
{
    [JsonProperty("type")]
    public string Type { get; set; }
}

public class HakerNewsStory
{
    [JsonProperty("by")]
    public string By { get; set; }

    [JsonProperty("descendants")]
    public long Descendants { get; set; }

    [JsonProperty("id")]
    public long Id { get; set; }

    [JsonProperty("kids")]
    public long[] Kids { get; set; }

    [JsonProperty("score")]
    public long Score { get; set; }

    [JsonProperty("time")]
    public long Time { get; set; }

    [JsonProperty("title")]
    public string Title { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("url")]
    public Uri Url { get; set; }
}

// Use Libraries like RestSharp to fetch the Data
// Find the Type
var typeID = JsonConvert.DeserializeObject<HakernewsTypeIdentifier>(json);
// Deserialize once again based on the Type
if(typeID.type == "story")
    var story = JsonConvert.DeserializeObject<HakerNewsStory>(json);
公共类HakerNewTypeIdentifier
{
[JsonProperty(“类型”)]
公共字符串类型{get;set;}
}
公开课客家故事
{
[JsonProperty(“由”)]
由{get;set;}生成的公共字符串
[JsonProperty(“后代”)]
公共长子体{get;set;}
[JsonProperty(“id”)]
公共长Id{get;set;}
[JsonProperty(“儿童”)]
公共长[]子对象{get;set;}
[JsonProperty(“分数”)]
公共长记分{get;set;}
[JsonProperty(“时间”)]
公共长时间{get;set;}
[JsonProperty(“所有权”)]
公共字符串标题{get;set;}
[JsonProperty(“类型”)]
公共字符串类型{get;set;}
[JsonProperty(“url”)]
公共Uri Url{get;set;}
}
//使用诸如RestSharp之类的库来获取数据
//找到类型
var typeID=JsonConvert.DeserializeObject(json);
//根据类型再次反序列化
if(typeID.type==“故事”)
var story=JsonConvert.DeserializeObject(json);

希望这能有所帮助。

有一个名为TypeIdentifier的原始POCO,它只有一个单独识别类型的类型,然后使用原始POCO

用于将任何JSON转换为C#类(POCO),并使用反序列化

public class HakernewsTypeIdentifier
{
    [JsonProperty("type")]
    public string Type { get; set; }
}

public class HakerNewsStory
{
    [JsonProperty("by")]
    public string By { get; set; }

    [JsonProperty("descendants")]
    public long Descendants { get; set; }

    [JsonProperty("id")]
    public long Id { get; set; }

    [JsonProperty("kids")]
    public long[] Kids { get; set; }

    [JsonProperty("score")]
    public long Score { get; set; }

    [JsonProperty("time")]
    public long Time { get; set; }

    [JsonProperty("title")]
    public string Title { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("url")]
    public Uri Url { get; set; }
}

// Use Libraries like RestSharp to fetch the Data
// Find the Type
var typeID = JsonConvert.DeserializeObject<HakernewsTypeIdentifier>(json);
// Deserialize once again based on the Type
if(typeID.type == "story")
    var story = JsonConvert.DeserializeObject<HakerNewsStory>(json);
公共类HakerNewTypeIdentifier
{
[JsonProperty(“类型”)]
公共字符串类型{get;set;}
}
公开课客家故事
{
[JsonProperty(“由”)]
由{get;set;}生成的公共字符串
[JsonProperty(“后代”)]
公共长子体{get;set;}
[JsonProperty(“id”)]
公共长Id{get;set;}
[JsonProperty(“儿童”)]
公共长[]子对象{get;set;}
[JsonProperty(“分数”)]
公共长记分{get;set;}
[JsonProperty(“时间”)]
公共长时间{get;set;}
[JsonProperty(“所有权”)]
公共字符串标题{get;set;}
[JsonProperty(“类型”)]
公共字符串类型{get;set;}
[JsonProperty(“url”)]
公共Uri Url{get;set;}
}
//使用诸如RestSharp之类的库来获取数据
//找到类型
var typeID=JsonConvert.DeserializeObject(json);
//根据类型再次反序列化
if(typeID.type==“故事”)
var story=JsonConvert.DeserializeObject(json);

希望这有帮助。

我不知道这是否是最好的解决方案

反序列化代码

var item=JsonConvert.DeserializeObject(responseString);
开关(项目类型)
{
案例类型。工作:
返回JsonConvert.DeserializeObject(responseString);
案例类型。故事:
返回JsonConvert.DeserializeObject(responseString);
案例类型。注释:
返回JsonConvert.DeserializeObject(responseString);
案例类型。投票:
返回JsonConvert.DeserializeObject(responseString);
case Type.PollOpt:
返回JsonConvert.DeserializeObject(responseString);
违约:
抛出新ArgumentOutOfRangeException();
}
所有项目类别

公共类ItemType
{
公共类型类型{get;set;}
}
公共枚举类型
{
没有一个
工作,
故事
评论,,
投票
波洛普,
}
公共接口项
{
}
公开课故事:IItem
{
由{get;set;}生成的公共字符串
公共长子体{get;set;}
公共长Id{get;set;}
公共列表子项{get;set;}
公共长记分{get;set;}
公共长时间{get;set;}
公共字符串标题{get;set;}
公共字符串类型{get;set;}
公共Uri Url{get;set;}
}
公共类评论:IItem
{
由{get;set;}生成的公共字符串
公共长Id{get;set;}
公共列表子项{get;set;}
公共长父项{get;set;}
公共字符串文本{get;set;}
公共长时间{get;set;}
公共字符串类型{get;set;}
}
公开课工作:项目
{
由{get;set;}生成的公共字符串
公共长Id{get;set;}
公共长记分{get;set;}
公共字符串文本{get;set;}
公共长时间{get;set;}
公共字符串标题{get;set;}
公共字符串类型{get;set;}
公共字符串Url{get;set;}
}
公开课投票:IItem
{
由{get;set;}生成的公共字符串
公共长子体{get;set;}
公共长Id{get;set;}
公共列表子项{get;set;}
公共列表部分{get;set;}
公共长记分{get;set;}
公共字符串文本{get;set;}
公共长时间{get;set;}
公共字符串标题{get;set;}
公共字符串类型{get;set;}
}
公共类PollOpt:IItem
{
由{get;set;}生成的公共字符串
公共长Id{get;set;}
公共长轮询{get;set;}
公共长记分{get;set;}
公共字符串文本{get;set;}
公共长时间{get;set;}
公共字符串类型{get;set;}
}

我不知道这是否是最好的解决方案

反序列化代码

var item=JsonConvert.DeserializeObject(responseString);
开关(项目类型)
{
案例类型。工作:
返回JsonConvert.DeserializeObject(responseString);
案例类型。故事:
返回JsonConvert.DeserializeObject(responseString);
案例类型。注释:
返回JsonConvert.DeserializeObject(responseString);
案例类型。投票:
返回JsonConvert.DeserializeObject(responseString);
case Type.PollOpt:
返回JsonConvert.DeserializeObject(responseString);
违约:
抛出新ArgumentOutOfRangeException();
}
所有项目类别

公共类ItemType
{
公共类型{get;