0

JSON.Net では、親クラス (アクセス権がない) から継承されたフィールドを無視するようにデシリアライゼーション プロセスを取得するにはどうすればよいですか。

JSON フィードに、システム クラスから継承されたものと同じ名前のフィールドがあります。そして、デシリアライズしようとすると、これが原因で失敗します (正確なエラーメッセージ:

A member with the name 'Location' already exists on 'Client.JSON.MyClass'. Use the JsonPropertyAttribute to specify another name.

Location は、システム クラスでもある親クラスで定義されているため、JsonIgnore 属性を定義するためにそのクラスにアクセスすることはできません。

JSON Location プロパティを MyClass の継承元のシステム クラスに逆シリアル化しようとせずに逆シリアル化を実行できるように、これをバイパスするにはどうすればよいですか?

最後に、JSON フィードは WCF Data Services を使用して生成されるため、'd' ルート配列が含まれています。 ):

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace Client.JSON
{
    public class MyClassContractResolver : DefaultContractResolver
    {
        protected override IList<JsonProperty> CreateProperties(JsonObjectContract t)
        {
            IList<JsonProperty> properties = base.CreateProperties(t);

            properties =
                properties.Where(p => p.PropertyName.StartsWith('J'.ToString())).ToList();

            return properties;
        }
    }
}

そして、次のコードを使用して逆シリアル化します。

jsonAppointments = JsonConvert.DeserializeObject<RootMyClass>(myJsonString, new JsonSerializerSettings { ContractResolver = new MyClassContractResolver() });

誰かがこれを行う方法を知っていれば、それは大歓迎です! ありがとう。ちなみに、これはCompact Frameworkを使用しています。

4

1 に答える 1