Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Create JSON by ignoring a few properties of a class

$
0
0

Hi,

I need urgent help. I would like to create the following structure in JSON using C# in a WebAPI:

{"locations": [
{"latitude": 19.0844,"longitude": 72.8360
}, {"latitude": 19.0830,"longitude": 72.8406
}, {"altitude": 10.0,"latitude": 19.0830,"longitude": 72.8406,"relevantText": "You are near INOX"
}
]

}

I have created the following class:

private class location
    {
        public double latitude { get; set; }
        public double longitude { get; set; }
        public double altitude { get; set; }
        public string relevantText { get; set; }
    }

I am using it as below:

passList Objpasslist = new passList();
IList<location> lslocation = new List<location>();
object setobj;


lslocation.Add(new location
        {
            latitude = 19.0844,
            longitude = 72.8360,
        });
        lslocation.Add(new location
        {
            latitude = 19.0830,
            longitude = 72.8406,
        });
        lslocation.Add(new location
        {
            altitude = 10.0,
            latitude = 19.0830,
            longitude = 72.8406,
            relevantText = "You are near INOX"
        });

        Objpasslist.locations = lslocation;


setobj = Objpasslist;
        JavaScriptSerializer js = new JavaScriptSerializer();

        string strJSON = JsonConvert.SerializeObject(setobj, Formatting.Indented);
        strJSON = strJSON.TrimStart('[');
        strJSON = strJSON.TrimEnd(']');

The output that i currently get is:

{"locations": [
{"latitude": 19.0844,"longitude": 72.836,"altitude": 0.0,"relevantText": null
},
{"latitude": 19.083,"longitude": 72.8406,"altitude": 0.0,"relevantText": null
},
{"latitude": 19.083,"longitude": 72.8406,"altitude": 10.0,"relevantText": "You are near INOX"
}
]

}


How to get the desired output in C#?


Viewing all articles
Browse latest Browse all 4850

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>