简体   繁体   中英

Serializing JSON in .NET

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.

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

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.

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

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