0

I have a problem to read json to object:

{
    "root":[
        {
        "arr1":[
          {
              "name":"n1",
              "age": "a2"
          },
          {
              "name":"n2",
              "age":"a2"
          }
       ]},
       {
       "arr2":[
           {
               "name":"n11",
               "age" : "age22"
           },
           {
               "name":"n12",
               "age" :"a21"
           }
       ]}
    ]
 }

It seemed to me it could work on this way:

public class Root
{
    public List<Arr> myArr{ get; set; }
}
public class Arr
{
     public Dictionary<string, List<Info>> info{ set; get; }
}
public class Info
{
     public string name{get;set;}
     public string age {get;set;}
}

Root root = JsonConvert.DeserializeObject<Root>(json);

This will return Root object which will have 3 objects, but each of the objects (Arr) would be null. Obviously I am doing something wrong, but I have no idea what. Ideally, it would be even better to get just Dictionary> as final result

NOTE: Example above is just simple example, there will be more "arrX" objects, I need it to be loaded dynamically.

Jonhtra
  • 897
  • 2
  • 12
  • 18
  • The names must match *exactly*. You have two named arrays in your sample json, "arr1" and "arr2" which do not show up in your c# class. – Travis J Jan 10 '17 at 21:09
  • Well, that is the point, there will be more "arrX", I need it dinamicaly to load in dictionary – Jonhtra Jan 10 '17 at 21:09
  • I am almost sure it is possible, there is something similar, but it simply does not work for me: http://stackoverflow.com/questions/24771804/how-can-i-deserialize-a-child-object-with-dynamic-numeric-key-names – Jonhtra Jan 10 '17 at 21:15
  • That question has nothing to do with this question. The structures are totally different – Sir Rufo Jan 10 '17 at 21:17
  • Update your question with how you tried to use the answer(s) in that other question and how they didn't work (what error messages you got, etc.) – Heretic Monkey Jan 10 '17 at 21:17
  • @MikeMcCaughan How should that work at all? The other question is dealing with a lot of properties and here we have an array? – Sir Rufo Jan 10 '17 at 21:20
  • @Sir Rufo Yes, it is different, but there are also dynamic keys in that question ... – Jonhtra Jan 10 '17 at 21:20
  • @Mike McCaughan I alraedy mentioned that I managed to load the object and in root.myArr I have 2 objects (I wrote 3 in my question, my bad) which are null. – Jonhtra Jan 10 '17 at 21:20
  • @SirRufo Dynamic properties in both cases. The array is a something to be considered, but certainly doesn't rule out the possibility of using the technique share there. – Heretic Monkey Jan 10 '17 at 21:22
  • @Jonhtra Yes, but even if you have that dynamic keys, the structure is not comparable and so you cannot use that solution – Sir Rufo Jan 10 '17 at 21:22
  • Are you generating the json? age as string is...a bit odd – Ňɏssa Pøngjǣrdenlarp Jan 10 '17 at 21:23
  • @Plutonix Why would that even matter, if you think it will change something, I can make it int, double? – Jonhtra Jan 10 '17 at 21:24
  • 2
    Just use `public class Root { public List>> root { get; set; } }` – Kalten Jan 10 '17 at 21:24
  • As mentioned previously, you have no `myArr` property on your root object. Your class structure would have to be something like `class MyObj { root: List {get;set;} }` – Heretic Monkey Jan 10 '17 at 21:25
  • Well a bunch of **different** Json Objects (thats what they are, because of the different property names) is somehow different to a Json Object with a bunch of properties – Sir Rufo Jan 10 '17 at 21:25
  • @SirRufo If you lack the ability to see how the linked question could be useful in this circumstance, feel free to bow out of the discussion. The OP has seen the usefulness of it, which is all that really matters. – Heretic Monkey Jan 10 '17 at 21:27
  • @Kalten Thank you, that worked perfectly, can you please add it as answer – Jonhtra Jan 10 '17 at 21:29
  • Thats the answer linked above. go upvote it – Ňɏssa Pøngjǣrdenlarp Jan 10 '17 at 21:31

2 Answers2

2

Just use :

public class RootObject
{
    public List<Dictionary<string, List<Info>>> Root { get; set; }
}

public class Info
{
    public string Name { get; set; }
    public string Age { get; set; }
}

And then :

var root = JsonConvert.DeserializeObject<RootObject>(json);
Kalten
  • 4,092
  • 23
  • 32
0

Try the following set of classes

public class Rootobject
{
    public Root[] root { get; set; }
}

public class Root
{
    public Arr1[] arr1 { get; set; }
    public Arr2[] arr2 { get; set; }
}

public class Arr1
{
    public string name { get; set; }
    public string age { get; set; }
}

public class Arr2
{
    public string name { get; set; }
    public string age { get; set; }
}

Root root = JsonConvert.DeserializeObject<Rootobject>(json);

If you want to clean up the class a bit you can specify the Json property names using [JsonProperty(Name = "ayx)].

Kevin Smith
  • 13,746
  • 4
  • 52
  • 77