简体   繁体   中英

JSON to Dictionary c#

[
  {
    "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"
      }
    ]
  }
]

I want to convert this JSON to dictionary. but I'm getting some Exception while converting this.

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

Error:

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

To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg 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.

This JSON doesn't seem to describe a dictionary. Instead, it seems to describe a List of Dictionaries containing one dictionary, whose key is "CRMNextFields" and whose value is another List of Dictionaries.

Thus, deserializing into this works:

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

And accessing the inner dictionaries like this:

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

You should be able to track this logic visually. The outermost container is a [], so you know it's a JSON Array, not a JSON Object. The member of this array is {}, so it's an object (ie convertible to Dictionary in C#), and that object has one property with a key and a value which is, again, a [] array, so you'll keep following the same logic.

You can do like in this answer: https://stackoverflow.com/a/16339352/1805974

Deserialize to dynamic object:

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

usage:

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

that way it acts almost like a dictionary. Dunno if you make it a ExpandoObject, but it is basically an IDictionary

Your JSON is not a Dictionary<object,object> . You can use JSON2CSHARP for check structure of your JSON. It should look like this.

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; }
}

And now you can deserialize it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM