简体   繁体   中英

Json object de-serialization to c# model

I have a json return from third party webservice in this format. I'm little worry about its de-serialization to some object. what should be my structure of a model?

{"123":{"Name":"test1","Address":"add 1"}
,"1412":{"Name":"test2","Address":"add 2"}
,"4123":{"Name":"test3","Address":"add 3"}}
public class __invalid_type__123
{
    public string Name { get; set; }
    public string Address { get; set; }
}

public class __invalid_type__1412
{
    public string Name { get; set; }
    public string Address { get; set; }
}

public class __invalid_type__4123
{
    public string Name { get; set; }
    public string Address { get; set; }
}

public class RootObject
{
    public __invalid_type__123 __invalid_name__123 { get; set; }
    public __invalid_type__1412 __invalid_name__1412 { get; set; }
    public __invalid_type__4123 __invalid_name__4123 { get; set; }
}

I obtained this by using the json2csharp tool. You'll notice that C# types can't start with a number, so they got "invalid type" prepended to them. But feel free to rename them whatever you want. This should get you started on the structure.

I know you have no control over the JSON, but it appears to be a poor choice of formatting. You have three types that are essentially the same. They should have used an array for that. Then it'd be much simpler.

Thanx @mason you give me a clue to find. I found a solution finally.. and i thought i should share it to you people too.

For detail reference

Answer to my question will be:

var jobj = (JObject)JsonConvert.DeserializeObject(json);
var items = jobj.Children()
    .Cast<JProperty>()
    .Select(j=>new
    {
        Id=j.Name,
        Name= (string)j.Value["Name"],
        Address= (string)j.Value["Address"]            
    })
    .ToList();

Using Json.Net you can simply:

public class NameAddress
{
    public string Name { get; set; }
    public string Address { get; set; }
}

public class RootObject
{
    [JsonProperty("123")]
    public NameAddress { get; set; }
    [JsonProperty("1412")]
    public NameAddress { get; set; }
    [JsonProperty("4123")]
    public NameAddress { get; set; }
}

I know this is old, but for those searching for the answer I have a solution. Using Json.Net I cracked this nut by deserializing the structure as a dictionary with a string key and an object value. The solution is something like this:

public class Item {
    public string Name { get; set; }
    public string Address { get; set; }
}

Dictionary<string, Item> itemList = 
    JsonConvert.DeserializeObject<Dictionary<string, Item>>( myJsonString );

I verified the above code works on your example. You can probably use another type of collection supported by Json.Net if you cannot guarantee the item names (or keys in the resulting dictionary) are not unique.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM