-1

I generate a JSON file for a city which contains the Interesting Place information of that city. My JSON file look like this-

{
  "Flensburg":[


    {
    "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"

    }

    ]


  }

Now in Model I created two classes POI.cs and RootObject.cs to get the object from this Json. These two classes look like this-

 namespace Test2_search.Models
 {
   public class POI
   {
    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; }
    }
 }

namespace Test2_search.Models
{
 public class RootObject
 {
    public List<POI> poi { get; set; }
 }
}

Now in the controller I at first implement a HttpPost to write the name in TextBox and then I created ActionResult GMap where I deserialize JSON data which I stored in APP_data.I want to get name in dynamic way.So that if I write in textbox Berlin, it will show all the deserialized json data for Berlin. if I write Flensburg it will show all the deserialized JSON data for Flensburg. The code I wrote for this method is-

namespace Test2_search.Controllers
{
 public class HomeController : Controller
  {
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(City objCityModel)
    {
        string name = objCityModel.Name;
        return View();
    }

    public ActionResult GMap(City objCityModel)
    {

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

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

        ViewBag.Name= json.poi.First().Name;
        ViewBag.ShortText = json.poi.First().Shorttext;
        ViewBag.Latitude =json.poi.First().Latitude;
        ViewBag.Longitude =json.poi.First().Longitude;
        ViewBag.Image =json.poi.First().Image;


        return View();
     }


  }
 }

Now in Index.cshtml I implement a textbox to write the name of the city-

@model  Test2_search.Models.City
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm("GMap", "Home"))
{
  <div class="wrapper wrapper-content">

    <div class="row">
        <div class="col-sm-12">
            @Html.TextBoxFor(m => m.Name)
            <label for="somevalue">City Name</label><input type="submit" id="City" name="City" value="Search" />
        </div>
    </div>
</div>
}

after writing the name in textbox it will go to Gmap.cshtml. In this page I want to show all the deserialized data from json file.

@model  Test2_search.Models.City
@{
ViewBag.Title1 = "Google Map View";
 }

 <h2>@ViewBag.Title</h2>
 <p>@ViewBag.ShortText</p>
 <p>@ViewBag.Latitide</p>
 <p>@ViewBag.ShortText</p>

But it is not working. it shows error -Source Error:

Line 45:             var json = JsonConvert.DeserializeObject<RootObject>   (ReadJson);
 Line 46: 
 Line 47:             ViewBag.Name= json.poi.First().Name;
 Line 48:             ViewBag.ShortText = json.poi.First().Shorttext;
 Line 49:             ViewBag.Latitude =json.poi.First().Latitude;
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • What error do you get? – Joakim Skoog Feb 09 '16 at 12:26
  • @JoakimSkoog I showd below my code. Source error –  Feb 09 '16 at 12:28
  • tamrezh21: You are not showing what **type** of error you're getting, only where the error is happening. – Joakim Skoog Feb 09 '16 at 12:29
  • It cannot get any viewbag message which i deserialized from json data –  Feb 09 '16 at 12:29
  • @JoakimSkoog I am getting error Stack Trace: [ArgumentNullException: Value cannot be null. Parameter name: source] System.Linq.Enumerable.First(IEnumerable`1 source) +4373858 Test2_search.Controllers.HomeController.GMap(City objCityModel) in c:\C# tutorial Backup\Test2_search\Test2_search\Controllers\HomeController.cs:47 lambda_metho... –  Feb 09 '16 at 12:32

2 Answers2

1

You have wrong Json format. It should be like:

{
    "poi": [
        {
            "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"
        }
    ]
}
emilpytka
  • 763
  • 7
  • 19
  • after correcting the json File I get output. but only the first one. not for 3 places –  Feb 09 '16 at 12:46
  • because you are getting json.poi.First() <- So only first one? I checked it and in json i have object with 3 items in array. Maybe you should keep in ViewBag whole List instead one item. – emilpytka Feb 09 '16 at 12:49
  • Could you please tell me how can i do that. I am very new in handling this case. Thank you –  Feb 09 '16 at 12:54
1

You have mismatch between JSON and object models.

public class RootObject
{
 public string Name {get;set;}
 public List<POI> poi { get; set; }
}

Assuming you get a JObject from your JSON:

jObject.Properties().Select(p=>new RootObject{Name = p.Name, poi = p.Value.ToObject<List<POI>>())

You cannot deserialize your JSON into RootObject because JSON does not have property poi - it is keyed by POI container name :)

Full LinqPad test:

void Main()
{
    var jObject = Newtonsoft.Json.Linq.JObject.Parse(json);
    var objects = jObject.Properties().Select(p=>new RootObject{Name = p.Name, poi = p.Value.ToObject<List<POI>>()});
    objects.Dump();
}

// Define other methods and classes here


 string json = "{\r\n  \"Flensburg\":[\r\n\r\n\r\n    {\r\n    \"Name\": \"Flensburg Firth\",\r\n    \"Shorttext\": \"Flensburg Firth or Flensborg Fjord ....\",\r\n    \"Longitude\": 9.42901993,\r\n    \"Latitude\": 54.7959404,\r\n    \"Image\": \"https://upload.wikimedia.org/wikipedia/commons/thumb/8/8c/Flensborg_Fjord_ved_bockholmwik.jpg/400px-Flensborg_Fjord_ved_bockholmwik.jpg\"\r\n    },\r\n\r\n\r\n    {\r\n    \"Name\": \"Naval Academy M\u00FCrwik\",\r\n    \"Shorttext\": \"The Naval Academy M\u00FCrwik is the main train....\",\r\n    \"Longitude\": 9.45944444,\r\n    \"Latitude\": 54.815,\r\n    \"Image\": \"https://upload.wikimedia.org/wikipedia/commons/thumb/f/fb/MSM-hauptgebaeude.jpg/400px-MSM-hauptgebaeude.jpg\"\r\n\r\n    },\r\n    {   \r\n    \"Name\": \"Nordertor\",\r\n    \"Shorttext\": \"The Nordertor is an old town gate in Flensburg, Germany....\",\r\n    \"Longitude\": 9.43004861,\r\n    \"Latitude\": 54.79541778,\r\n    \"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\"\r\n\r\n    }\r\n\r\n    ]\r\n\r\n\r\n  }";

public class POI
   {
    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 string Name {get;set;}
    public List<POI> poi { get; set; }
 }
Gerino
  • 1,943
  • 1
  • 16
  • 21
  • I have a question.how can I get This output in viewbag message –  Feb 09 '16 at 12:51
  • If you change the RootObject definition and replace `var json = JsonConvert.DeserializeObject(ReadJson);` with `var json = JObject.Parse(ReadJson).Properties().Select(p=>new RootObject{Name = p.Name, poi = p.Value.ToObject>()});` it might just work. But I'd suggest spending a moment to understand what was the problem and what's actually happening in the code ;) – Gerino Feb 09 '16 at 12:57
  • It's LinqPad (https://www.linqpad.net/) method, that shows an object in results window. It's an useful tool for quickly checking some .NET code without creating a new solution in VisualStudio, and the posted snippet can be directly pasted to LinqPad and executed. You only need to add a `Name` property to `RootObject` and then replace the line with `var json =...`, nothing else. – Gerino Feb 09 '16 at 13:10