2017-06-15 3 views
0
私はNewtonSoftのJSON 4.5と次のようなJSON文字列に解析しようとしている

の値を使用します。私は、JSONにシリアライズされますプロパティ名をスキップし、のみJSON

{ 
    "Name":"Henrik", 
    "Children":[ 
    { 
     "Name":"Adam", 
     "Grandchildren":[ 
     "Jessica", //How can i only show the value and not the property name? 
     "Michael" 
     ] 
    } 
    ] 
} 

私のオブジェクトは次のようになります。

public class Parent 
{ 
    public string Name { get; set; } 

    [JsonProperty(PropertyName = "Children")] 
    public List<Child> Childs { get; set; } 
} 

public class Child { 
    public string Name { get; set; } 

    [JsonProperty(PropertyName = "Grandchildren")] 
    public List<GrandChild> GrandChilds { get; set; } 
} 

public class GrandChild { 
    [JsonProperty(PropertyName = "")] 
    public string Name { get; set; } 
} 

プロパティ名を空に設定しようとしましたが、問題を解決しませんでした。

+0

の可能性のある重複[Json.Net:いないオブジェクトとして、値としてプロパティをデシリアライズ/シリアライズ(https://stackoverflow.com/questions/40480489/json-net-serialize-deserialize-property-オブジェクトとして価値のないものとして) – dbc

答えて

1

List<GrandChild>List<string>に置き換えるか、GrandChildのようにカスタムJsonConverterを追加する必要があります。

[JsonConverter(typeof(GrandChildConverter))] 
public class GrandChild 
{ 
    public string Name { get; set; } 
} 

public class GrandChildConverter : JsonConverter 
{ 
    public override bool CanConvert(Type objectType) 
    { 
     return objectType == typeof(GrandChild); 
    } 

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
     writer.WriteValue(((GrandChild)value).Name); 
    } 

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     return new GrandChild { Name = reader.Value.ToString() }; 
    } 
} 
関連する問題