2016-10-10 16 views
-1
[ 
    { 
    "CRMNextFields": [ 
     { 
     "FieldName": "ActiveTime", 
     "CastRequired": "False", 
     "DefaultValue": "", 
     "Description": "", 
     "FieldID": "10001567", 
     "KeyID": "6", 
     "Label": "SN_ActiveTime", 
     "IsMandatory": "False", 
     "MaxValue": "", 
     "MinValue": "", 
     "Type": "1", 
     "IsLookup": "False", 
     "LinkedFields": "", 
     "FieldKey": "0", 
     "TextFieldName": "", 
     "ErrorMessage": "", 
     "LayoutFieldId": "", 
     "LayoutType": "0", 
     "IsDNC": "", 
     "DNCField": "", 
     "ReturnType": "-1" 
     }, 
     { 
     "FieldName": "CreatedByTypeName", 
     "CastRequired": "False", 
     "DefaultValue": "", 
     "Description": "", 
     "FieldID": "10001601", 
     "KeyID": "6", 
     "Label": "SN_CreatedByType", 
     "IsMandatory": "False", 
     "MaxValue": "", 
     "MinValue": "", 
     "Type": "2", 
     "IsLookup": "True", 
     "LinkedFields": "", 
     "FieldKey": "257", 
     "TextFieldName": "", 
     "ErrorMessage": "", 
     "LayoutFieldId": "", 
     "LayoutType": "-1", 
     "IsDNC": "", 
     "DNCField": "", 
     "ReturnType": "-1" 
     } 
    ] 
    } 
] 

このJSONを辞書に変換したいと思います。 しかし、私はこれを変換中にいくつかの例外を取得しています。JSONから辞書へ#

Dictionary<object, object> values = JsonConvert.DeserializeObject<Dictionary<object, object>>(jsonResp); 

エラー:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.Object,System.Object]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

Path '', line 1, position 1.

+0

デシリアライズのために 'Dictionary 'を試してください。しかし、あなたは辞書よりもはるかに大きなオブジェクト、つまり辞書スタイルのフォーマットを持つオブジェクトを持つ配列を持っているようです。あなたはそこの辞書でこれを非直列化できるクラスを作ることができます – Icepickle

答えて

1

このJSONは辞書を記述していないようです。代わりに、キーが "CRMNextFields"であり、その値が辞書の別のリストである辞書を含む辞書のリストを記述しているようです。

このように、本の中にデシリアライズして動作します:

var data = JsonConvert 
.DeserializeObject<List<Dictionary<string, List<Dictionary<string,object>>>>> 
(/*your json*/); 

そして、このような内部辞書にアクセス:

var crmFields = data.First()["CRMNextFields"]; 
foreach (Dictionary<string,object> crmField in crmFields) 
{ 
    foreach (var innerProperty in crmField) 
    { 
     // this will iterate the innermost fields. 
    } 
} 

あなたが視覚的にこのロジックを追跡することができるはずです。最も外側のコンテナは[]であるため、JSONオブジェクトではなくJSON配列であることがわかります。この配列のメンバーは{}なので、オブジェクト(つまり、C#でDictionaryに変換可能)です。そのオブジェクトはキーと値を持つ1つのプロパティを持ちます。同じ論理。

0

あなたはこの答えのように行うことができます。動的オブジェクトへ https://stackoverflow.com/a/16339352/1805974

デシリアライズ:

dynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonstring); 

使用:それはほとんどの辞書のような役割を果たし

string fieldnameFromFirstField =json.CRMNextFields[0].FieldName; 

そのように。 DunnoをExpandoObjectにすると基本的にIDictionaryになります

-1

Dictionary<object,object>ではありません。 JSONのチェック構造にはJSON2CSHARPを使用できます。このように見えるはずです。

public class CRMNextField 
{ 
    public string FieldName { get; set; } 
    public string CastRequired { get; set; } 
    public string DefaultValue { get; set; } 
    public string Description { get; set; } 
    public string FieldID { get; set; } 
    public string KeyID { get; set; } 
    public string Label { get; set; } 
    public string IsMandatory { get; set; } 
    public string MaxValue { get; set; } 
    public string MinValue { get; set; } 
    public string Type { get; set; } 
    public string IsLookup { get; set; } 
    public string LinkedFields { get; set; } 
    public string FieldKey { get; set; } 
    public string TextFieldName { get; set; } 
    public string ErrorMessage { get; set; } 
    public string LayoutFieldId { get; set; } 
    public string LayoutType { get; set; } 
    public string IsDNC { get; set; } 
    public string DNCField { get; set; } 
    public string ReturnType { get; set; } 
} 

public class RootObject 
{ 
    public List<CRMNextField> CRMNextFields { get; set; } 
} 

これでデシリアライズできます。

var data = JsonConvert.DeserializeObject<List<RootObject>>(yourJSON);