2

I want to get data from json file correctly. The json data file I modeled for this is as follows:

{
    "Free title 1":[
        {
            "Subject": "a1",
            "Relation": "a2"
        },
        {
            "Subject": "b1",
            "Relation": "b2"
        }
    ],
    "Another free title":[
        {
            "Subject": "z1",
            "Relation": "z2"
        },
        {
            "Subject": "y1",
            "Relation": "y2"
        }
    ],
    "Unordered title":[
        {
            "Subject": "x1",
            "Relation": "x2"
        },
        {
            "Subject": "w1",
            "Relation": "w2"
        }
    ]
}

This is how I create an object class:

public class _Infos_
{
    public List<_Info_> Infos { get; set; }
}
public class _Info_
{
    public string Subject { get; set; }
    public string Relation { get; set; }
}

And finally I'm trying to get the data in a method like this:

var js = JsonConvert.DeserializeObject<_Infos_>(File.ReadAllText("__FILE_PATH__"));
foreach (var j in js.Infos)
{
    MessageBox.Show(j.Subject);
}

I get the error that js is empty. Here I want to get Free title 1, Another free title and Unordered title in a list. Of course, these titles will be constantly changing. Afterwards, I want to get the Subject and Relation data under these titles. But I have no idea how to get it.

1
  • 2
    You need something like JsonConvert.DeserializeObject<List<_Info_>> per outer object member. Else the outer object must have one key with the same name as the property Infos. I.e. if you are using dynamic naming, the deserialization will not work like this. Commented Mar 10, 2022 at 8:08

3 Answers 3

3

This data structure is a dictionary of collections of _Info_s. You need to deserialize it to Dictionary<string, List<_Info_>>.

Here are System.Text.Json and Json.net examples:

var d = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, List<_Info_>>>(json);

var d2 = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, List<_Info_>>>(json);
Sign up to request clarification or add additional context in comments.

Comments

2

Your class definition is a little wrong. You can use online tools "json to c#" to generate the correct classes. like this one: https://json2csharp.com

Your "root" of your json for example does not contain an array in your json. The property "Free title 1":[..] is an array, so your root needs a property with the name FreeTitle1 and it has to be an array/list.

public class Root
{
    [JsonProperty("Free title 1")]
    public List<TitleInfo> FreeTitle1 { get; set; }

    [JsonProperty("Another free title")]
    public List<TitleInfo> AnotherFreeTitle { get; set; }

    [JsonProperty("Unordered title")]
    public List<TitleInfo> UnorderedTitle { get; set; }
}

public class TitleInfo
{
    public string Subject { get; set; }
    public string Relation { get; set; }
}

2 Comments

Thank you for the update. Likely the names/keys of the members are not known in advance. How would you in that case approach the issue?
What if you add another book?
1

If your object members have dynamic names, you can also manually deserialize the object, e.g. using the general type JObject. E.g.

JObject obj = JObject.Parse(File.ReadAllText("__FILE_PATH__"));

JObject implements IEnumerable<KeyValuePair<string, JToken>> over which you can iterate. Each member will then have JToken Value, which is a JArray in this case, which you can cast to a List of your type.

foreach (var groups in obj)
{
    var infos = groups.Value.ToObject<List<_Info_>>();
    // .. loop over infos
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.