-1

Hey how can you turn json obects into a dictionary or something like that?

This is my json body:

 {
"name": "person1",
"listOne": [{
    "name": "list1",
    "description": "",
    "listTwo": [{
        "name": "object1"
    }, {
        "name": "object2"
    }, {
        "name": "object3"
    }]
}, {
    "name": "list2",
    "description": "",
    "listTwo": [{
        "name": "object1"
    }, {
        "name": "object2"
    }]
}]

}

This are the classes:

 public class JsonObject()
 {
      public string Name { get; set;}
      public string Description { get; set;}
      public List<string> MyList { get; set; }
 }

 public class MyList()
 {
      public string Name { get; set; }
 }

The problem is that I cannot select the Name property from MyList! On my xaml page I see App1.Model.MyList.

How can I show the value from the property Name from the class MyList?

In my ViewModel this is my List that I return to the xamlpage:

 private ObservableCollection<JsonObject> _ObjectList = new ObservableCollection<JsonObject>();
 public ObservableCollection<JsonObject> ObjectList
 {
     get
     {
         return _ObjectList;
     }
     set
     {
         _ObjectList = value;
         RaisePropertyChanged("ObjectList");
     }
 }

So I thought maybe create a disctionary and add thise values to it, but I can't reach the property Name from MyList class....

Bayern
  • 330
  • 3
  • 20

2 Answers2

0

You can use a service like http://json2csharp.com to generate a C# class structure for any JSON.

In this case, it would be something like this:

public class ListTwo
{
    public string name { get; set; }
}

public class ListOne
{
    public string name { get; set; }
    public string description { get; set; }
    public List<ListTwo> listTwo { get; set; }
}

public class RootObject
{
    public string name { get; set; }
    public List<ListOne> listOne { get; set; }
}

If you change the casing / name of the properties, you have to add a DataContractAttribute with the corrent name which is used in the JSON model. Of course, it depends on your serializer if you need that.

nikeee
  • 10,248
  • 7
  • 40
  • 66
  • We both got the same thing :O – Amit Kumar Ghosh Dec 04 '15 at 12:46
  • @AmitKumarGhosh this is not the problem I can get all items in my rootObject. It is on the viewmodel that i can't reach the property "Name" from listTwo object. It is a list into a list. On my xaml page I see instead of the name the objectclass. – Bayern Dec 04 '15 at 13:26
  • @Bayern Take a look at this line in your model: `public List MyList { get; set; }`. The MyList list is a List here, so it doesn't have a name property. I think you have to change it to be a `List` or use the model provided in the answer above. – nikeee Dec 04 '15 at 13:55
0

Why no just deserialize to identical c# types -

public class ListTwo
{
   public string name { get; set; }
}

public class ListOne
{
    public string name { get; set; }
    public string description { get; set; }
    public List<ListTwo> listTwo { get; set; }
}

public class RootObject
{
    public string name { get; set; }
    public List<ListOne> listOne { get; set; }
}

and the just deserialize it -

var typed = JsonConvert.DeserializeObject<RootObject>(json);
Amit Kumar Ghosh
  • 3,618
  • 1
  • 20
  • 24