2016-06-27 21 views
0

JSONの表記は少し複雑ですが、これは特にJSONとの最初の出会いです。 3番目のツールを使用して、JSON内のすべての関連オブジェクトと配列のC#クラスを作成しましたが、一部の内部オブジェクトはスキップされます。私は、欠けているオブジェクトをコピーし、それらのクラスを作成し、それに応じて使用しました。JSON解析の問題 - C#

ほとんどのデータを解析してアクセスできましたが、ほとんどの内部オブジェクトは解析されていません。

私にとってのハードルの1つは、JSONに同じ名前、つまり「値」を持つ複数の名前/値のペアが含まれていることです。

JSONエキス:

"controls": [ 
        { 
        "value": "<p>The Governments current tobacco control strategy Creating a Tobacco-Free Generation: A Tobacco Control Strategy for Scotland sets out the Governments vision for a tobacco-free Scotland by 2034. Our contribution to this strategy is through</p>\n<ul>\n<li>prevention</li>\n<li>protection</li>\n<li>smoking cessation services.</li>\n</ul>\n<p>Tobacco smoking is a major risk factor for</p>\n<ul>\n<li>coronary heart disease</li>\n<li>stroke</li>\n<li>peripheral vascular disease</li>\n<li>many respiratory conditions including COPD</li>\n<li>a range of cancers </li>\n</ul>\n<p>and many other diseases and conditions. It is a leading cause of preventable ill health, premature death and disability.</p>\n<p>The Governments strategy outlines the importance of reducing health inequalities and the role that reducing smoking rates in the most deprived communities can have on this.</p>", 
        "editor": { 
         "alias": "rte" 
        } 
        }, 
        { 
        "value": [ 
         { 
         "callToAction": { 
          "value": [ 
          { 
           "name": "Read Government’s tobacco control strategy (external website)", 
           "url": "http://www.gov.gov/Publications", 
           "target": "_blank", 
           "icon": "icon-link" 
          } 
          ], 
          "dataTypeGuid": "5befdwe546fb12-3bc2-4f39-bab3-7328dfg345382afe65", 
          "editorAlias": "callToAction", 
          "editorName": "URL" 
         } 
         } 
        ], 
        "editor": { 
         "alias": "callToAction" 
        }, 
        "guid": "e3as34dc2831-c4e8-8671-038c-15634see45de7b71a" 
        }, 
        { 
        "value": "<h2>Reducing tobacco-related inequalities </h2>\n<p>The European Regional Office of the World Health Organisation (WHO) has produced policy guidance focusing on reducing inequalities relating to tobacco smoking. The guidance looks at</p>\n<ul>\n<li>the widening socio-economic inequities in tobacco consumption</li>\n<li>how the use of tobacco contributes to health inequalities</li>\n<li>the complexity of smoking in terms of inequality</li>\n<li>how tobacco increases inequalities over the life course</li>\n<li>what tobacco policies need to consider to address inequalities.</li>\n</ul>\n<p>The guidance reports that</p>\n<ul>\n<li>A child born in a more socially deprived area of Scotland is more likely to be born into a family that smokes, is more likely to have a mother who smoked during her pregnancy and is more likely to be around smokers growing up. Children of smokers are more likely to start smoking themselves and continue to repeat the cycle of their own experience.</li>\n<li>People living in the most deprived areas are less likely to feel in control of their life, more likely to experience stress and mental health issues and be less aware of the harm of smoking and second hand smoke.</li>\n<li>When it comes to stopping smoking this group of smokers are more likely to smoke heavily and have a stronger nicotine dependence and therefore find it harder to stop. They are less likely to know where to get help, and have less encouragement and social support to quit.</li>\n<li>They are more likely to suffer financial hardship as a consequence of smoking related illness, more likely to live in poor housing and more likely to have other health problems made worse by smoking.</li>\n</ul>\n<p>You can read the full <a href=\"http://ec.europa.eu/health/social_determinants/key_documents/index_en.htm#anchor1\">‘Tobacco and inequities’ guidance</a> on the European Commission’s website (external website).</p>\n<p>Our tobacco control work is achieved by working in partnership with Government, local and national NHS Boards along with <a href=\"http://www.ashscotland.org.uk/\" target=\"_blank\">ASH Scotland</a> (external website) including the <a href=\"http://www.ashscotland.org.uk/alliances\" target=\"_blank\"> Tobacco-free Alliance</a> (external website).</p>\n<h2>Getting help with local tobacco planning</h2>\n<p>To help with local planning, Public Health Observatory() have created local tobacco control profiles which include indicators on </p>\n<ul>\n<li>adult smoking prevalence</li>\n<li>smoking during and post-pregnancy</li>\n<li>smoking cessation</li>\n<li>smoking related death and disease</li>\n<li>tobacco sales.</li>\n</ul>", 
        "editor": { 
         "alias": "rte" 
        } 
        }, 
        { 
        "value": [ 
         { 
         "callToAction": { 
          "value": [ 
          { 
           "name": "Visit the TEST online profiles tool (external website)", 
           "url": "http://www.TEST.org.uk/comparative-health/profiles/online-profiles-tool", 
           "target": "_blank", 
           "icon": "icon-link" 
          } 
          ], 
          "dataTypeGuid": "5bs3a232-3bc2-4f39-bab3-73283ewr82afe65", 
          "editorAlias": "callToAction", 
          "editorName": "URL" 
         } 
         } 
        ], 
        "editor": { 
         "alias": "callToAction" 
        }, 
        "guid": "25ese341-4c05-0c11-e643-3f3aderd84e8e3" 
        } 
       ] 

私は最初のオブジェクト({"value":"<p>The ....", "editor":{...}} を得ることができますが、続く第2のオブジェクトにアクセスすることはできませんし、callToActionを持って

すべてのヘルプは非常になります

私は必要に応じてコードを共有できます。私はC#& Newtonsoft.Json。

を使用しています。 クラス:ここ

using Newtonsoft.Json; 

namespace CallToAction.Models 
{ 
    public class Control 
    { 

     [JsonProperty("value")] 
     public Value[] Value { get; set; } 

     [JsonProperty("editor")] 
     public Editor Editor { get; set; } 

    } 
} 

using Newtonsoft.Json; 

namespace CallToAction.Models 
{ 
    public class Value 
    { 

     [JsonProperty("callToAction")] 
     public CallToAction CallToAction { get; set; } 
    } 
} 

using Newtonsoft.Json; 

namespace CallToAction.Models 
{ 
    public class CallToAction 
    { 

     [JsonProperty("value")] 
     public Value2[] Value { get; set; } 

     [JsonProperty("dataTypeGuid")] 
     public string DataTypeGuid { get; set; } 

     [JsonProperty("editorAlias")] 
     public string EditorAlias { get; set; } 

     [JsonProperty("editorName")] 
     public string EditorName { get; set; } 
    } 
} 

そして、私のローカルマシン上のファイルからJSONを解析しているコードの行がある

@jeffのメルカドは、ここのコードです。

Control values = JsonConvert.DeserializeObject<Control>(System.IO.File.ReadAllText(@"C:\\Folder\\CallToAction\\CallToAction\\Content\\Value.json")); 
+0

_必ず_コードを共有してください。 –

+0

このサイトにアクセスし、アクセスする方法を知ることができます - http://www.jsoneditoronline.org/ –

+0

ありがとうございますarun、しかしサイトはタイムアウトしています。 – LiaqatG

答えて

1

あなたの"controls": [{...}, {...}]配列が多型で表示されます。それは型がネストされたトークン"editor.alias"によって示され、各種の制御を含むことができます。これをクラス階層としてモデル化し、Deserializing polymorphic json classes without type information using json.netの行に沿って逆シリアル化することができます。次のように

あなたのポリモーフィッククラス階層をモデル化することができます。

[JsonConverter(typeof(ControlBaseConverter))] 
public abstract class ControlBase 
{ 
    public Editor editor { get; set; } 
} 

public class Editor 
{ 
    public string alias { get; set; } 
} 

public class CallToActionValue 
{ 
    public string name { get; set; } 
    public string url { get; set; } 
    public string target { get; set; } 
    public string icon { get; set; } 
} 

public class CallToAction 
{ 
    public List<CallToActionValue> value { get; set; } 
    public string dataTypeGuid { get; set; } 
    public string editorAlias { get; set; } 
    public string editorName { get; set; } 
} 

public class CallToActionControlValue 
{ 
    public CallToAction callToAction { get; set; } 
} 

[JsonConverter(typeof(NoConverter))] 
public class CallToActionControl : ControlBase 
{ 
    public List<CallToActionControlValue> value { get; set; } 

    public string guid { get; set; } 
} 

[JsonConverter(typeof(NoConverter))] 
public class RteControl : ControlBase 
{ 
    public string value { get; set; } 
} 

public class RootObject 
{ 
    public List<ControlBase> controls { get; set; } 
} 

custom JSON convertersを使用する場合:[

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

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     if (reader.TokenType == JsonToken.Null) 
      return null; 
     var obj = JObject.Load(reader); 
     var editorValue = (string)obj.SelectToken("editor.alias"); 

     Type type; 
     if (string.Equals(editorValue, "rte", StringComparison.OrdinalIgnoreCase)) 
     { 
      type = typeof(RteControl); 
     } 
     else if (string.Equals(editorValue, "callToAction", StringComparison.OrdinalIgnoreCase)) 
     { 
      type = typeof(CallToActionControl); 
     } 
     else 
     { 
      throw new JsonSerializationException("Unknown type of Control: " + editorValue); 
     } 
     return obj.ToObject(type, serializer); 
    } 

    public override bool CanWrite { get { return false; } } 

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
     throw new NotImplementedException(); 
    } 
} 

public class NoConverter : JsonConverter 
{ 
    public override bool CanConvert(Type objectType) 
    { 
     return false; 
    } 

    public override bool CanRead { get { return false; } } 

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     throw new NotImplementedException(); 
    } 

    public override bool CanWrite { get { return false; } } 

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
     throw new NotImplementedException(); 
    } 
} 

そして実行します。

var root = JsonConvert.DeserializeObject<RootObject>(jsonString); 

は、派生クラスに[JsonConverter(typeof(NoConverter))]を追加します無限の再帰を防ぐベース型コンバータは派生型を逆シリアル化しようとします。それ以外の場合は[JsonConverter(typeof(ControlBaseConverter))]属性が継承されます。

サンプルfiddle

+0

詳細な回答と努力のために@dbcいただきありがとうございます。これは私の実際の仕事に統合するのにはしばらくかかりましたが、これは大規模な複合体からの小さな(しかし、最も内側の)抽出物でしたが、整形式のJsonオブジェクトではありませんでした。 – LiaqatG