0

Which procedure and data-structure should I use that will give me access all the key-value pairs in a nested JSON string in C#?

Pictured below is a portion of the JSON string I am working with. As you can see it is nested. I am seeking to retrieve the "date" and "message" from each block with their respective keys using C-Sharp C#.

I have tried putting my JSON string into a Dictionary.

private static async Task<String> ProcessRepo()
{

   var stringTask = APIclient.GetStringAsync("https://a.RESTapi.fakecom"); 
   var x = await stringTask;
   Dictionary<string, object> data = JsonConvert.DeserializeObject<Dictionary<string, object>>(x); 

   return data;
}

This gives me 2 key-values pairs. One is "pagelen" with a value of 30, and the other is "values" with a value of everything that followed in one long string. Not very useful.

This is my first time using C#. Please point me in the right direction. I have spent many hours searching for something that will work.

enter image description here

Community
  • 1
  • 1
  • 2
    You can paste the JSON into Visual Studio and it will create *classes* which match the layout and data types of the JSON. Use **Edit** -> **Paste Special** -> **Paste JSON as classes** use that in place of `Object` in your Deserialize statement . Though I am not sure a Dictionary is ideal based on the fragment shown – Ňɏssa Pøngjǣrdenlarp Feb 17 '20 at 02:33
  • I don't have Visual Studio. I'm running Linux. I have access to Visual Studio Code. Does it have this feature? –  Feb 17 '20 at 03:15
  • You can also use json2csharp.com get the classes based on a json – Jawad Feb 17 '20 at 03:39
  • 1
    If you want to auto-generate classes for your JSON string, see [How to auto-generate a C# class file from a JSON string](https://stackoverflow.com/q/21611674/3744182). To load JSON into a document object model without a fixed schema, use `JToken.Parse()` or `JObject.Parse()` as shown in [this answer](https://stackoverflow.com/a/4749755/3744182) to [Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)](https://stackoverflow.com/q/4749639/3744182). – dbc Feb 17 '20 at 04:58

2 Answers2

2

Issue with the deserialization is that you are trying to deserialize the complete JSON as Dictionary of key value pairs but that was not how the JSON is, that is the reason why you could not access the values as expected/

Two ways to solve this case

Method 1:

var data = JsonConvert.DeserializeObject(x);

This will deserialize the complete json in an unstructured way, and you will get keys and values for each and every nested JSON.

Method 2: Create a class with the same structure as your JSON.

    public class MyJson
    {
        [JsonProperty("pageLen")]
        public int PageLen { get; set; }

        [JsonProperty("values")]
        public Values Values { get; set; }

    }

    public class Values
    {
        [JsonProperty("rendered")]
        public Rendered Rendered { get; set; }

        [JsonProperty("hash")]
        public string Hash { get; set; }
    }

    public class Rendered
    {

    }

And then pass this as deserializing class as below.

    private static async Task<String> ProcessRepo()
    {
         MyJson data = JsonConvert.DeserializeObject<MyJson>(x);
    }
0

The clear solution depends on your goals, but simplest way is something like this:

var obj = JsonConvert.DeserializeObject(json);
foreach (var jo in (JArray)((JObject)obj)["values"])
{
     Console.WriteLine($"{jo["message"]}, {jo["date"]}");
}
Rick
  • 11
  • 3