0

First of all, I'm new to MVC.

I want to display the properties of the JSON response in a html view.

For example, i want to get the number of page likes from the JSON response and display just the number of likes on a page.

Any help is much appreciated :)

    //
    // GET: /Facebook/

    public ActionResult Index()
    {
        var json = new WebClient().DownloadString("https://graph.facebook.com/google");
        JsonConvert.DeserializeObject<RootObject>(json);

        return view();
    }

    public class CategoryList
    {
        public string id { get; set; }
        public string name { get; set; }
    }

    public class Location
    {
        public string street { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string country { get; set; }
        public string zip { get; set; }
        public double latitude { get; set; }
        public double longitude { get; set; }
    }

    public class Cover
    {
        public string cover_id { get; set; }
        public string source { get; set; }
        public int offset_y { get; set; }
        public int offset_x { get; set; }
    }

    public class RootObject
    {
        public string about { get; set; }
        public string awards { get; set; }
        public string category { get; set; }
        public List<CategoryList> category_list { get; set; }
        public int checkins { get; set; }
        public string company_overview { get; set; }
        public string description { get; set; }
        public string founded { get; set; }
        public bool is_published { get; set; }
        public Location location { get; set; }
        public string mission { get; set; }
        public string phone { get; set; }
        public string products { get; set; }
        public int talking_about_count { get; set; }
        public string username { get; set; }
        public string website { get; set; }
        public int were_here_count { get; set; }
        public string id { get; set; }
        public string name { get; set; }
        public string link { get; set; }
        public int likes { get; set; }
        public Cover cover { get; set; }
    }
}
}
SteveO
  • 3
  • 1
  • 2
  • 1
    Store your likes in a `ViewData["likes"] = likes;` in your action(Index) and call it in your index view to display it. `@ViewData["likes"] likes`. Carles solution also works fine. – Karthik Chintala Nov 12 '13 at 12:54

3 Answers3

1

Your action should pass the object to the view:

public ActionResult Index()
{
    var json = new WebClient().DownloadString("https://graph.facebook.com/google");
    var root=JsonConvert.DeserializeObject<RootObject>(json);

    return view(root);
}

and then in your view you can show whichever property you want:

@Model RootObject
<html>
    <head>
        <title>Showing properties</title>
    </head>
    <body>
        @Model.likes likes.
    </body>
</html>

This is if you use the Razor syntax.

Carles Company
  • 7,118
  • 5
  • 49
  • 75
0

you're missing

return view(root);

You should pass the object back to view to use it.

you can use JsonResult in mvc 4,

  public JsonResult ReturnSomeJson()
  {
   JsonResult result = new JsonResult();
   //Assign some json value to result.
   //Allow get is used to get the value in view.
   return view(result,AllowGet.True);
  }
Sakthivel
  • 1,890
  • 2
  • 21
  • 47
  • Thanks for your response codebarin, Carles answer does work for me. However is your answer an industry standard practice? I am very new to MVC. Should i stick to what works or set the project up for easier and future development? – SteveO Nov 13 '13 at 08:45
  • When you're new to some technology its usual you stick to what's easy but i would recommend you learn quick and follow standards that wont break your development architecture. – Sakthivel Nov 13 '13 at 08:47
0

I was looking for a similar solution and I found it to be a bit different:

[HttpGet]
public JsonResult Index() {
    // your code
    return Json("some result string or value", JsonRequestBehavior.AllowGet);
}

This outputs "some result string or value" in your browser when you call this action directly.

Assuming this is a normal action inside a controller that inherits from Controller class.

nrod
  • 398
  • 6
  • 17