1

I need to deserialize this weird JSON (image below). I've seen some deserialization hints using Dictionary<>, etc. but the problem is that "parameters" contains different data, then previous keys.

Can I somehow get it to work using JsonSerializer deserializator without doing foreach loops and other suspicious implementations? I do need data from "data" in my application.

Here's some of my code:

using var client = new WebClient();
var json = client.DownloadString(GetJsonString());
var invoicesData = JsonSerializer.Deserialize<JsonMyData>(json, options);

If using Newtonsoft is necessary I might start using it.

Text

4

3 Answers 3

2

With Newtonsoft you can parse and access arbitrary JSON documents, even ones that can't reasonably be deserialized into a .NET object. So something like:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

namespace ConsoleApp35
{
    class Program
    {
        static void Main(string[] args)
        {
            var json = @"
{
                ""myData"" : 
    {
      ""0"" : { ""data"": { ""A"":1,""B"":2} },
      ""1"" : { ""data"": { ""A"":1,""B"":2} },
      ""2"" : { ""data"": { ""A"":1,""B"":2} },
      ""3"" : { ""data"": { ""A"":1,""B"":2} },
      ""parameters"" : { ""p"":""a""}
    },
 ""status"":{ }
}";

            var foo = JObject.Parse(json);
            var a = foo["myData"]["1"]["data"];
            Console.WriteLine(a);

            Console.WriteLine("Hit any key to continue");
            Console.ReadKey();
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I'll try it later and see what I've ended up with, thanks EDIT: I meant I need a collection of all "data" entries. So basically I have to foreach with everything? Full json is like +1000 entries of data
Since you're transforming a JSON object into a collection, yes, you'll have to iterate them and add them to a collection.
Thanks a lot, at the end I figured out I could just remove "parameters" via JToken.Last.Remove() and then deserialized everything in foreach loop. Your reply was very helpful :)
1

I think you should really consider using Newtonsoft.Json instead of default JsonDeserializer, it is much easier to use in such situations.

Comments

0

If you are interested in processing this without foreach loops and wanting to access the data in a list format, I would suggest using Dictionary for this. When you use dictionary, you can use Objects as values that would compensate for differences in numbers (0, 1, 2, ..) and words (parameters).

    // Classes to Deserialize data we need.
    public class MyObject
    {
        [JsonProperty("data")]
        public Data Data { get; set; }
    }

    public class Data
    {
        public int A { get; set; }
        public int B { get; set; }
    }

Usage in Main

    // Read in the JSON
    var myData = JsonConvert.DeserializeObject<dynamic>(jsonString)["myData"];

    // Convert To Dictionary
    Dictionary<string, dynamic> dataAsObjects = myData.ToObject<Dictionary<string, dynamic>>();

    string searchFor = "3";
    dataAsObjects.TryGetValue(searchFor, out dynamic obj);
    if (obj != null)
    {
        // Conversion to int and matching against searchFor is to ensure its a number.
        int.TryParse(searchFor, out int result);
        if (result == 0 && result.ToString().Equals(searchFor))
        {
            MyObject myObject = obj.ToObject<MyObject>();
            Console.WriteLine($"A:{myObject.Data.A} - B:{myObject.Data.B}");
        }
        else if (result == 8 && result.ToString().Equals(searchFor))
        {
            // I am not clear on whats your parameters class look like.
            MyParameters myParams = obj.ToObject<MyParameters>();
        }
    }

Output

A:1 - B:2

With this method you can either access the numbers or the parameters element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.