0

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"}}
2
  • I would start with a class like: public class ContactData { int id; string name; string address; } Commented Apr 29, 2014 at 13:31
  • 2
    Take a look at stackoverflow.com/questions/7590088/…. I had a similar problem. This solution worked perfectly for me. Commented Apr 29, 2014 at 13:45

4 Answers 4

1
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.

Sign up to request clarification or add additional context in comments.

3 Comments

@MareInfinitus Please explain, as saying it's not correct alone doesn't tell me what's incorrect about it.
From what I read in the question, OP is searching for a way to generate an object graph. You deliver three types for three equally formed json objects. This does not seem to be what OP wants, at least for me.
The OP asked for what the structure of the object(s) should be that he's deserializing to. I provided a one to one example of how to deserialize it. I pointed out that the three equal types were due to the structure of the provided JSON. It appears they were intended by the provider to be separate types, else an array would have been used.
0

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();

1 Comment

While this looks like it will work, you should look at Erik's answer, as it gives you a nice object you can reuse, and the deserialization only takes one line of code. Json.NET is a great library.
0

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; }
}

Comments

0

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.

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.