Possible JSON for a Dictionary in C #

I'm a little happy with the concept of JSON

s, so bear with me here.

I have a method that returns the following long and complex string:

[{\"identifierType\":\".Identifier\",\"title\":\"Test\",\"typeName\":\"Application1\",\"key\":\"1\",\"internalId\":1},{\"identifierType\":\".Identifier\",\"title\":\"Test2\",\"typeName\":\"Application2\",\"key\":\"2\",\"internalId\":15},{\"identifierType\":\".Identifier\",\"title\":\"Test3\",\"typeName\":\"Application3\",\"key\":\"3\",\"internalId\":1124},{\"identifierType\":\".Identifier\",\"title\":\"Test4\",\"typeName\":\"Application4\",\"key\":\"4\",\"internalId\":2241}]

      

She has a long and ugly eyesore, but this is something I need to work with.

After a bit of searching, I discovered that it was a string JSON

and that it could be matched against Dictionary

in C#

.

Now, after a bit more searches, I ended up with this:

Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(s);

      

In this case s

, my long and ugly line.

The problem is that it throws an error on startup.

It is not possible to deserialize the current JSON array (eg [1,2,3]) to type 'System.Collections.Generic.Dictionary`2 [System.String, System.String] 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 the collection interface (eg ICollection, IList) as a List that can be deserialized from a JSON array. The JsonArrayAttribute can also be added to the type to make it deserialize from a JSON array.

Path '', line 1, position 1.

Now that I was green as cabbage in JSON

I was dumbfounded by this.

I am not very pretty s

non- JSON

string, I thought it was?

If it is, shouldn't it then theoretically work?

I also foresee a problem with the fact that s

there is more than one object in it. Doesn't this cause problems when I try to match it with Dictionary

?

Do I need to match s

against multiple Dictionary

s?

Please advise.

+3


source to share


3 answers


Your json is a let say array YourJsonObject

, so you can deserialize it to List<YourJsonObject>

.

The http://json2csharp.com site comes in handy in these scenarios, so by inserting json into it, it produces the following class:

public class YourJsonObject
{
    public string identifierType { get; set; }
    public string title { get; set; }
    public string typeName { get; set; }
    public string key { get; set; }
    public int internalId { get; set; }
}

      



and

var list = JsonConvert.Deserialize<List<YourJsonOject>>(s);

gets what you want. If you really wanted to be able to deserialize it in a list of dictionaries, as the commenter suggested above me, but since it contains an integer, it might be better.

+3


source


First, let's decorate your JSON. I used the JsonEditorOnline round trip feature, this site is very useful for one-off exploration of JSON data:

[
  {
    "identifierType": ".Identifier",
    "title": "Test",
    "typeName": "Application1",
    "key": "1",
    "internalId": 1
  },
  {
    "identifierType": ".Identifier",
    "title": "Test2",
    "typeName": "Application2",
    "key": "2",
    "internalId": 15
  },
  {
    "identifierType": ".Identifier",
    "title": "Test3",
    "typeName": "Application3",
    "key": "3",
    "internalId": 1124
  },
  {
    "identifierType": ".Identifier",
    "title": "Test4",
    "typeName": "Application4",
    "key": "4",
    "internalId": 2241
  }
]

      

JSON has the nice property that if you know the basics, it's very easy for people to read. The [ ]

parentheses indicate an array and the curly braces indicate { }

objects.
In this case, you have an array of objects, properties identifierType

, title

, typeName

, key

and internalId

.

The easiest way to convert this to data that you can work with in C # is to create a class that matches this structure:



public class MyItem // pick a better name, of course
{
    [JsonProperty("identifierType")]
    public string IdentifierType { get; set; }

    // add the other properties in the same fashion
}

      

Then you can convert this JSON to your data structure:

var items = JsonConvert.DeserializeObject<List<MyItem>>( yourJsonString );

      

+2


source


Your 'not' json.

Its a Json array of Json objects. C # is talking about a dictionary.

For eg -

JsonArray:

[{"d": 1}, {"y": 1}, {"z": 3}]

The solution can be parsed into a json array and then the foreach value will create a dictionary.

Json object:

{"a": 1, "b": 2}

-1


source







All Articles