-2

I am a beginner in .Net framework, and I want to know how I can return a model without matching all the parameters in the json data. For example I have an unknown json coming in but I do know that there will be a "Name" and "Nickname" value, so I want to create an object model from these values.

NoctisLupus27
  • 71
  • 1
  • 7
  • Does this answer your question? [Deserialize JSON into C# dynamic object?](https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object) – Filburt Aug 27 '21 at 18:51

2 Answers2

1

Is this what you mean?

    public class MyClass
    {
        public string key { get; set; }
        public NameArray[] array { get; set; }
    }

    public class NameArray
    {
        public string Name { get; set; }
    }

    static void Main(string[] args)
    {
        string jsonString = "{\"key\": \"myKey\", \"array\": [{\"Name\": \"John\"},{\"Name\": \"Jack\"}]}";
        var myClass = JsonSerializer.Deserialize<MyClass>(jsonString);

        Console.WriteLine($"key: {myClass.key}");
        Console.WriteLine($"name0: {myClass.array[0].Name}");
        Console.WriteLine($"name1: {myClass.array[1].Name}");
    }
Toni78
  • 97
  • 6
0

You need a class that has your values and that's what JsonSerializer will use to deserialize the json string.

    public class MyClass
    {
        public string Name { get; set; }
        public string Nickname { get; set; }
    }

    static void Main(string[] args)
    {
        string jsonString = "{\"firstName\": \"ABC\", \"lastName\": \"XYZ\", \"age\": 5,  \"email\":\"abcxyz @blahblah.com\", \"Name\": \"ABC XYZ\", \"Nickname\": \"AZ\"}";
        var myClass = JsonSerializer.Deserialize<MyClass>(jsonString);

        Console.WriteLine($"Name: {myClass.Name}");
        Console.WriteLine($"Nickname: {myClass.Nickname}");
    }
Toni78
  • 97
  • 6
  • The things i i want something that does not need MyClass, because each entry has too many values to create a specific class for it, I just want to take my values like firstName and feed it somewhere else. – NoctisLupus27 Aug 27 '21 at 19:07
  • Your question states that you have a json with many fields but no matter what, there will be a Name and Nickname in that json string. I understand that those field names could be different but the assumption on my part was that you have some known parts in the json string. And that's what the code is extracting. If the fields Name and Nickname are also unknown, then Filburt's comment may be a more appropriate answer than mine. Are you looking to deserialize a json string that you have no prior information to then create a structure or class that is also unknown and undefined at compile time? – Toni78 Aug 27 '21 at 19:14
  • Actually your answer is very close to what I need. Basically the json result has the following form: {key: value, array: [Name: "John", etc.., Name: "Jack"]}. How can I follow the same suit with your answer if I want john and jack? – NoctisLupus27 Aug 27 '21 at 19:22
  • Make the member an array. If you have a json field that is ... "SomeArray: [Name: "John", Name: "Jack"] you need to have another class SomeArray with the member Name in it. Then MyClass would have public SomeArray[] someArray instead of public string Name. – Toni78 Aug 27 '21 at 19:32