Jaroslaw Matlak
Jaroslaw Matlak

Reputation: 574

Deserialize json with multiple records, unknown keys and unknown number of keys

I need to deserialize json file with unknown keys and unknown ammount of keys

{"key1":"val01", "key2":"val02", "key3":"val03", ..., "keyn":"val0n"}
{"key1":"val11", "key2":"val12", "key3":"val13", ..., "keyn":"val1n"}
{"key1":"val21", "key2":"val22", "key3":"val23", ..., "keyn":"val2n"}

I don't know the keys and even don't know n.

I've tried to parse it as list of dictionaries:

res = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(jsontext);

but I've received an exception:

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code

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

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

Path '/APL/HVAC/TREG/EACT', line 1, position 23..

How to get rid of this exception?

Upvotes: 0

Views: 1671

Answers (4)

Jaroslaw Matlak
Jaroslaw Matlak

Reputation: 574

I've dealt with this problem using this:

var result = new List<Dictionary<string, string>>();
var jsons = jsontext.Split('\n');
foreach (var x in jsons)
{
    var res = JsonConvert.DeserializeObject<Dictionary<string, string>>(x);
    result.Add(res);
}

Upvotes: 0

HebeleHododo
HebeleHododo

Reputation: 3640

Your json is not valid. You can validate your json strings with tools like JSON Formatter

Lists should have [ in the beginning and ] in the end. You also need to put commas between elements.

A valid version of your string would be like this:

[
    {
        "key1": "val01",
        "key2": "val02",
        "key3": "val03",
        "keyn": "val0n"
    }, 
    {
        "key1": "val11",
        "key2": "val12",
        "key3": "val13",
        "keyn": "val1n"
    }, 
    {
        "key1": "val21",
        "key2": "val22",
        "key3": "val23",
        "keyn": "val2n"
    }
]

JSON Editor Online lets you both check and edit.

Upvotes: 3

Yanga
Yanga

Reputation: 3012

your json file is maybe malformed, it should start with [ and end with ] to be readable with a Dictionary<string, string>

Exemple:

[{"key1":"val01"}, {"key2":"val02"}, {"key3":"val03"}, ..., {"keyn":"val0n"}]

Upvotes: 0

fafl
fafl

Reputation: 7385

Try this:

using Newtonsoft.Json.Linq;

string json = "[{\"key1\":\"val01\", \"key2\":\"val02\", \"key3\":\"val03\", \"keyn\":\"val0n\"}, {\"key1\":\"val11\", \"key2\":\"val12\", \"key3\":\"val13\", \"keyn\":\"val1n\"}, {\"key1\":\"val21\", \"key2\":\"val22\", \"key3\":\"val23\", \"keyn\":\"val2n\"}]";
var objects = JArray.Parse(json); // parse as array
foreach(JObject obj in objects)
{
    foreach(KeyValuePair<String, JToken> pair in obj)
    {
        Console.WriteLine(pair.Key + " -> " + pair.Value);
    }
}

Upvotes: 0

Related Questions