Xamarin Forms - 自定义的可观察集合 JSON

Xamarin Forms - Observable Collection to CUSTOM JSON

我有一个可观察的集合,其中包含绑定到 ListView 的产品列表。

但是我想将这个 Observable Collection 导出为 JSON 文件并且只导出特定条目,这样我就可以通过 API.

提交它

例如

完整的可观察集合包含

但我只想将 JSON 文件提取到:

这是我的代码:

public static ObservableCollection<FBProduct> fbproducts = new ObservableCollection<FBProduct>();

这是我的 JSON 解串器

shoppingcartjson = JsonConvert.SerializeObject(ShoppingCart.fbproducts);

我怎样才能像这样只从 ObservableCollection 中提取 ProductID 和 ProductQTY:

"line_items": [{"product_id":79631,"quantity":1}],

在你的 FBProduct class 使用 JsonIgnore 属性很简单!

例如:

 public class FBProduct
 {    
    [JsonIgnore]
    public double Name  { get; set; } 
    .
    .

此外,添加以下 using 语句:

using Newtonsoft.Json;

祝你好运!

如有任何问题,请随时回来。

如果您想在序列化 FBProduct 时始终忽略 属性,请继续使用 FreakyAli 的答案。我将快速解释一下如何仅在某些时候忽略属性。


有时您想要忽略某些属性,而其他时候您想要完整 class 而不忽略任何属性。但是通过将 [JsonIgnore] 属性放在 属性 上,您将总是 忽略它,这不是很好。因此,Newtonsoft 提供了一种有条件地忽略属性的方法,使用他们所谓的契约解析器。您可以实现自己的合同解析器,以便有时能够以编程方式忽略属性(以及使用它们的属性执行的所有其他操作)。
以下是如何实现有条件地忽略某些属性的合同解析器:

public class IgnorePropertyContractResolver : DefaultContractResolver
{
    // Holds our information for which properties to ignore on which types
    private readonly Dictionary<Type, HashSet<string>> _ignores;

    public IgnorePropertyContractResolver()
    {
        _ignores = new Dictionary<Type, HashSet<string>>();
    }

    public void IgnoreProperty(Type type, params string[] propertyNames)
    {
        // If we don't know the type to ignore properties on, initialize the HashSet
        if (_ignores.ContainsKey(type))
            _ignores[type] = new HashSet<string>();
 
        foreach (var prop in propertyNames)
            _ignores[type].Add(prop);
    }

    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        // Create the property as normal
        var property = base.CreateProperty(member, memberSerialization);

        // If we're ignoring the property
        if (IsIgnored(property.DeclaringType, property.PropertyName))
        {
            // Don't serialize and ignore
            property.ShouldSerialize = i => false;
            property.Ignored = true;
        }

        return property;
    }


    private bool IsIgnored(Type type, string propertyName)
    {
        // If we're not ignoring any property on the type, return false
        if (!_ignores.ContainsKey(type))
            return false;

        // If we are ignoring some properties on the type, return if we're ignoring the given property
        return _ignores[type].Contains(propertyName);
    }
}

然后我们使用这个自定义合约解析器如下:

var fbProduct = new FBProduct();
var resolver = new IgnorePropertyContractResolver();
resolver.IgnoreProperty(typeof(FBProduct),
    nameof(FBProduct.ProductID),
    nameof(FBProduct.Name),
    nameof(FBProduct.Price),
    nameof(FBProduct.Qty)
);

var serialized = JsonConvert.SerializeObject(
    fbProduct,
    Formatting.None, // You can choose any formatting you want
    new JsonSerializerSettings 
    {
        ContractResolver = resolver
    }
);