ViRALiC
ViRALiC

Reputation: 1549

Possible JSON to Dictionary in C#

I'm a little bit fresh to the concept of JSONs, so bear with me here.

I have a method that returns the following long and complicated 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's a long and ugly eyesore, but it's what I've got to work with.

After a little bit of googling, I discovered that this was a JSON-string, and that she could be mapped to a Dictionary in C#.

Now, after a little more googling, I arrived at this:

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

In this case s is my long and ugly string.

The problem is that this throws an error when running.

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]' 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.

Now me being green as a cabbage in JSON, I was rendered dumbstruck by this.

Is my not-so-beautiful s not the JSON-string I thought she was?

If she is, shouldn't this then in theory work?

I also foresee a problem with the fact that there appears to be more than one object in s. Won't that cause trouble when I'm trying to map it to a Dictionary?

Do I have to map s to multiple Dictionarys?

Please advise.

Upvotes: 3

Views: 1396

Answers (3)

Solal Pirelli
Solal Pirelli

Reputation: 1219

First, let's beautify your JSON. I used JsonEditorOnline's round-tripping function, that website is pretty useful for one-off investigations into 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 really easy to read by humans. The brackets [ ] indicate an array, and the braces { } are for objects.
In this case, you have an array of objects, whose properties are identifierType, title, typeName, key and internalId.

The easiest way to convert this to data 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 that JSON to your data structure:

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

Upvotes: 2

Tam&#225;s Deme
Tam&#225;s Deme

Reputation: 2228

Your json is an array of let's say YourJsonObject, so you could deserialize it into a List<YourJsonObject>.

The http://json2csharp.com site comes in handy in these scenairos, so by pasting your json there it emits 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 you the thing you want. If you'd really wanted to you could deserialize it into a List of Dictionaries as a commenter above me suggested, but as it contains an integer too it's probably better this way.

Upvotes: 3

Rohit Garg
Rohit Garg

Reputation: 598

Your 's' is not a json.

Its Json Array of Json Objects. In C# terms say list of dictionary.

For eg-

JsonArray:

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

Solution could be parse into json array and then foreach value create a dictionary.

Json Object:

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

Upvotes: -1

Related Questions