Deserialize to Dictionary

I’m dealing with a third party gateway and I’m trying to deserialize the response to a dictionary.

Here is a simplified example of what I’m getting back from that gateway.

I’m trying to figure out why the entries are null when using FromJson.

using System;
using ServiceStack;
using ServiceStack.Text;

namespace ServiceStackSupport
{
    public class CountryResponse
    {
        public Dictionary<string, object> CountryList;
    }
    
    internal class Program
    {
        static void Main(string[] args)
        {
            string json = "[\n    {\n        \"active\": \"true\",\n        \"country\": \"Brazil\",\n        \"note\": \"A note about Brazil\",\n        \"other1\": \"198765\",\n        \"other2\": \"\",\n        \"other3\": \"10\",\n        \"other9\": \"\"\n    },\n    {\n        \"active\": \"true\",\n        \"country\": \"Algeria\",\n        \"note\":     \"A note about Alegria\",\n        \"other4\": \"wtv\",\n        \"other8\": \"\",\n        \"other6\": \"-1\"\n    },\n    {\n        \"active\": \"true\",\n        \"country\": \"Cabodia\",\n        \"note\": \"A note about Cabodia\",\n        \"other3\": \"108\",\n        \"other8\": \"\",\n        \"other10\": \"-1\"\n    }\n]";

            JsConfig.Init(new Config {
                TextCase = TextCase.SnakeCase,
                PropertyConvention = PropertyConvention.Lenient
            });

            var obj = JSON.parse(json);

            var entriesAreAllNull = json.FromJson<List<CountryResponse>>();

            Console.WriteLine();
        }
    }
}

image

image

Only public properties are serializable to start with, so you should add { get; set; } to make it a public property.

You can use FromJson<T>() if you’re trying to deserialize into Typed POCOs, if you’re trying to deserialize into an untyped Dictionary<string,object> you should use JSON.parse() instead.

Ah sorry about that. My mistake when I copied paste .

English is my second language. Just to make sure I understand.

Even with the { get; set; }

json.FromJson<List<CountryResponse>>();

Is not expected to return a list of CountryResponse with the populated dictionaries in the above example correct?

@Math, the example JSON you have is an array of objects, eg,

[
    {
        "active": "true",
        "country": "Brazil",
        "note": "A note about Brazil",
        "other1": "198765",
        "other2": "",
        "other3": "10",
        "other9": ""
    },
    {
        "active": "true",
        "country": "Algeria",
        "note":     "A note about Alegria",
        "other4": "wtv",
        "other8": "",
        "other6": "-1"
    },
    {
        "active": "true",
        "country": "Cabodia",
        "note": "A note about Cabodia",
        "other3": "108",
        "other8": "",
        "other10": "-1"
    }
]

Where as the structure you are trying to deserialize is a List<CountryResponse> where CountryResponse itself has a single property of CountryList. Eg, it is expecting the following structure.

[
    {
	    "country_list": {
			"active": "true",
			"country": "Brazil",
			"note": "A note about Brazil",
			"other1": "198765",
			"other2": "",
			"other3": "10",
			"other9": ""
		}
    },
    {
		"country_list": {
			"active": "true",
			"country": "Algeria",
			"note":     "A note about Alegria",
			"other4": "wtv",
			"other8": "",
			"other6": "-1"
		}
    },
    {
		"country_list": {
			"active": "true",
			"country": "Cabodia",
			"note": "A note about Cabodia",
			"other3": "108",
			"other8": "",
			"other10": "-1"
		}
    }
]

The JSON and the typed structure don’t match which is why you aren’t seeing your objects populated.

1 Like