反序列化JSON时需要忽略NULL值 [英] Need to ignore NULL values when deserializing JSON

查看:699
本文介绍了反序列化JSON时需要忽略NULL值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSON形式的一些简单的股票数据,并将其绘制在图表上.除某些条目返回NULL值外,其他所有方法都工作正常,因为在该特定时间没有进行任何交易,因此没有价格数据.这会在图表线上产生缺口.

I'm consuming some simple stock data in the form of JSON and plotting it on a chart. All works fine, except that some entries return NULL values because at that particular minute in time no trades were taken and therefore no price data is available. This creates gaps on the chart line.

因此,如果"close"值为null,我想将包括"minute"和"volume"的整个块排除在ObservableCollection中之外,然后继续添加下一个不为null的值.JSON示例:

So if the "close" value is null, I want to exclude the entire block including the "minute" and "volume" from being added into the ObservableCollection, and just move on to include the next one whose values are not null. Example JSON:

{
"minute": "10:21",  
"close": null,      
"volume": 0,
},{     
"minute": "10:22",
"close": 47.56,
"volume": 6,
}       

我创建了一个jsonSettings属性,我已经看到人们谈论并声称可以使用它,但是它不起作用.代码如下:

I have created a jsonSettings property which I have seen people talk about and claim work, yet it is not working. The code looks like this:

var jsonSettings = new JsonSerializerSettings
        {
            NullValueHandling = NullValueHandling.Ignore,
            MissingMemberHandling = MissingMemberHandling.Ignore
        };

        string content = await _client.GetStringAsync(url); 
        var json_Data = JsonConvert.DeserializeObject<ObservableCollection<ChartData>>(content,jsonSettings); 
        viewModel.LineData = json_Data;

这是我的模特:

public class ChartData
{
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public string minute { get; set; }

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public double? close { get; set; }

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public int volume { get; set; }
}

public class ViewModel
{
    public ObservableCollection<ChartData> LineData { get; set; }

    public ViewModel()
    {
        LineData = new ObservableCollection<ChartData>();
    }
}

我尝试了许多在此处和此处发布的类似示例,但空值条目保留在json_Data中.有什么想法使它起作用吗?

I have tried numerous similar examples posted here and there and yet the null-value entries remain within the json_Data. Any ideas how to make it work?

谢谢!

推荐答案

NullValueHandling.Ignore 在序列化时将忽略模型相关属性的空值.

NullValueHandling.Ignore will ignore null values for the relevant property of your model when serializing.

反序列化时,您可以考虑将反序列化为 IEnumerable< ChartData> ,然后根据最终的自定义逻辑,使用Linq过滤掉不需要的对象:关闭== null的对象".

When deserialzing, you might consider deserializing to an IEnumerable<ChartData> then using Linq to filter out the objects you don't want, based on what is, after all, custom logic: "exclude objects that have close == null".

例如(未经验证的空中代码):

E.g. (untested air code):

var data = JsonConvert.DeserializeObject<IEnumerable<ChartData>>(content,jsonSettings)
    .Where(cd => cd.close != null)
    ;

var observableData = new ObservableCollection<ChartData>(data);

这篇关于反序列化JSON时需要忽略NULL值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆