Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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.

share|improve this question
up vote 0 down vote accepted
    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

share|improve this answer
    
Thanks a lot. You save my day. Many Many thanks – harry.luson 12 hours ago

First of all : .First() always returns only one element or exception.

To get all Names You can :

  • Show cities in list/table passing to view model as RootObject and in view loop through every item in RootObject.city

  • Join all Names using string.Join method and pass them to View with ViewBag as You did.

Example String.Join(", ",json.city.Select(c => c.Name); => Output : London, New York, Paris


About "always getting last element" : this Code will return only last element.

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

Cause at first iteration your ViewBag.Name will contain first element name. At second iteration you override ViewBag.Name value to secon element name. So at the end you will get last element name

share|improve this answer
    
I have edited my controller class. but now I get the last one. Could you please explain how can i get all the three values. I am really struggling a lot about this issue for two days. thank you – harry.luson 17 hours ago
    
But I am not getting any way to show all the result accordingly. I have browsed 100 times all the site. but have not got any perfect solution for this . – harry.luson 17 hours ago

Change your View to this

    @model  Test2_search.Models.City


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

            @foreach (var item in Model)
            {
    <h2>@item.Title</h2>
    <p>@item.ShortText</p>
    <p>@item.Latitude</p>
    <p>@item.Longitude</p>
    <p>@item.Image</p>
}

Change your controller action to this

    public ActionResult GMap()
    {

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

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

   return View(json.city);
}

I'm not sure where json.poi came into play. I don't see that you've defined it in your model.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.