Search code examples
c#jsonasp.net-mvcjson.netjson-deserialization

get all object json data in view using ASP.NET MVC and JSON.Net


I created a .json file which contain tourist places of a city. My json file look like this-

{
 "city":[
 {
 "Name": "Flensburg Firth",
 "Shorttext": "Flensburg Firth or Flensborg Fjord ....",
 "Longitude": 9.42901993,
 "Latitude": 54.7959404,
 "Image": "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8c/Flensborg_Fjord_ved_bockholmwik.jpg/400px-Flensborg_Fjord_ved_bockholmwik.jpg"
 },


 {
 "Name": "Naval Academy Mürwik",
  "Shorttext": "The Naval Academy Mürwik is the main train....",
 "Longitude": 9.45944444,
 "Latitude": 54.815,
 "Image": "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fb/MSM-hauptgebaeude.jpg/400px-MSM-hauptgebaeude.jpg"

 },
 {   
 "Name": "Nordertor",
 "Shorttext": "The Nordertor is an old town gate in Flensburg, Germany....",
 "Longitude": 9.43004861,
 "Latitude": 54.79541778,
  "Images":"https://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/Nordertor_im_Schnee_%28Flensburg%2C_Januar_2014%29.JPG/266px-Nordertor_im_Schnee_%28Flensburg%2C_Januar_2014%29.JPG"

 }

]

}

My Model class to get the object for this json data is -

 public class City
 {
    public string Name { get; set; }
    public string Shorttext { get; set; }
    public double Longitude { get; set; }
    public double Latitude { get; set; }
    public string Image { get; set; }

 }
 public class RootObject
 {

    public List<City> city { get; set; }
 }

Now in controller I create actionresult to return view in view bag message- // I have edited contrller class

    public ActionResult GMap(City objCityModel)
    {

        string name = objCityModel.Name;
        ViewBag.Title = name;


        var ReadJson = System.IO.File.ReadAllText(Server.MapPath(@"~/App_Data/"+name+".json"));

       RootObject json = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<RootObject>(ReadJson);



        foreach (var item in json.poi)
        {

            ViewBag.Name = item.Name;
            ViewBag.ShortText = item.Shorttext;
            ViewBag.Latitude = item.Latitude;
            ViewBag.Longitude = item.Longitude;
            ViewBag.Image = item.Image;
        }

    return View();

My GMap.cshtml is -

@model  Test2_search.Models.City


@{
ViewBag.Title1 = "Google Map View";
}

<h2>@ViewBag.Title</h2>
<p>@ViewBag.ShortText</p>
<p>@ViewBag.Latitude</p>
<p>@ViewBag.Longitude</p>
<p>@ViewBag.Image</p>

With this code I can get only first object of that json file. like only data for "Flensburg Firth". but I want to show the all three places in that city in view. How can i get all the deserialized json data in this case.


Solution

  •     Lit<City> mycities=new List<City>();
         foreach (var item in json.poi)
        {
            City obj=new City(){
            Name = item.Name,
            ShortText = item.Shorttext,
            Latitude = item.Latitude,
            Longitude = item.Longitude,
            Image = item.Image,
             };
           mycities.Add(obj);
        }
    

    // You can even have this in your ViewModel

         ViewBag.Cities=mycities;
    

    And in your view @Foreach(var city viewBag.Cities){ //Bind ur data }

    Hope this steps could help