1

I have wrote a basic web service using .net which I intend to use in a mobile app. It currently outputs Json however the structure it not quite what I need.

The models I've created

[DataContract]
class PoiList
{
    [DataMember]
    public List<Poi> Pois { get; set; }
}

[DataContract]
class Poi
{
    [DataMember]
    public string title { get; set; }
    [DataMember]
    public string latitude { get; set; }
    [DataMember]
    public string longitude { get; set; }
}

Then i added some test data:

PoiList poiList = new PoiList
    {
        Pois = new List<Poi>()
    };

    Poi p = new Poi
    {
        title = "whatever",
        latitude = "-2.45554",
        longitude = "52.5454645"
    };

    poiList.Pois.Add(p);

    p = new Poi
    {
        title = "weeee",
        latitude = "-2.45554",
        longitude = "52.5454645"
    };

    poiList.Pois.Add(p);

    string ans = JsonConvert.SerializeObject(poiList, Formatting.Indented);

This is what the returned string looks like:

{ "Pois": [ { "title": "shit", "latitude": "-2.45554", "longitude": "52.5454645" }, { "title": "weeee", "latitude": "-2.45554", "longitude": "52.5454645" } ] }

...and this is what I want the outputted Json to look like:

string TempString = @"{ ""pois"":
                        [{ ""poi"": 
                            {
                                ""title"": ""Test title"",  
                                ""latitude"": ""-2.4857856"",
                                ""longitude"": ""54.585656""
                            }},
                        { ""poi"":
                            {
                                ""title"": ""Halfords"",
                                ""latitude"": ""-2.575656"",
                                ""longitude"": ""53.5867856""
                    }}]}";

Basically the only difference being the "poi" next to each object in the list. Is there a simple way to include this? I should add I am using the newtonsoft.json package.

2
  • 2
    Is there a specific reason why you need that extra "poi" wrapper? It seems redundant. Commented Mar 5, 2012 at 20:59
  • No reason other than I'm new to using json and I could only figure out how to parse the json string back in to an array by explicitly giving a poi wrapper to each set of properties. I'm sure there is a better way, but right now I just need something that works. Commented Mar 5, 2012 at 22:35

3 Answers 3

1

I would suggest you go with the JSON that's being generated, as your target JSON contains unnecessary objects, IMO.

Your target JSON has an object with a single field "pois", which contains a list of objects, each with a single field "poi" that contains an object with the fields "title", "latitude", and "longitude".

To access a single title field, you would need to do the following:

poisObj.pois[0].poi.title

If you go with the JSON that's generated by your object structure, you would access a single title field like so:

poisObj.pois[0].title

That having been said, if you absolutely must target that JSON structure, you'll need another DataContract object, as follows:

[DataContract]
class PoiList
{
    [DataMember]
    public List<PoiPoi> Pois { get; set; }
}
[DataContract]
class PoiPoi
{
    [DataMember]
    public Poi poi { get; set; }
}

[DataContract]
class Poi
{
    [DataMember]
    public string title { get; set; }
    [DataMember]
    public string latitude { get; set; }
    [DataMember]
    public string longitude { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

0

No need to declare many tiny classes just to output a json string. You can create an anonymous object to serialize like below:

var obj = new { pois = new List<object>() };
obj.pois.Add(new { poi = new {title = "Test title", latitude = "-2.4857856", longitude = "54.585656" } });
obj.pois.Add(new { poi = new {title = "Halfords"  , latitude = "-2.4857856", longitude = "53.5867856" } });

string json = JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);

Console.WriteLine(json);

2 Comments

There may be valid reasons to use named classes server-side.
That's exactly what I was looking for; my app is now parsing the json string perfectly, many thanks.
0

Go with the default structure the only thing the "poi" gives you is what "type" the object is, which JSON doesn't really have a concept of. If you want to include type information, try :-

 var jsonSerializerSettings = new JsonSerializerSettings
                {
                    TypeNameHandling = TypeNameHandling.Objects,
                    TypeNameAssemblyFormat = FormatterAssemblyStyle.Full,                
                };
 var json = JsonConvert.SerializeObject(o, Formatting.None, jsonSerializerSettings);

this will give you a field _type_ which is useful especially if you have a list of various types of objects. Json.Net then knows how to recreate the objects using this _type_ field.

1 Comment

The question: this is what I want the outputted Json to look like. How do you relate it with "type"? Does your answer return the intended string?

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.