Silverlight, JSON deserlialisation
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: Silverlight, JSON deserlialisation

Threaded View

  1. #1
    Join Date
    Feb 2012
    Posts
    1

    Question Silverlight, JSON deserlialisation

    Hello All

    I'm working on a LinkedIn integration and I have a silverlight app returning results based on a CompanySearch in JSON. I've found JSON.net and I have used it successfully to deserialise some data. However, I'm having difficulty deserialising a portion of the JSON result from LinkedIn. Essentially, I want to create a bunch of dynamic checkboxes which will eventually allow for a faceted search based on whether they are selected. The JSON result comming back from LinkedIn contains these facets in two buckets with values. I cannot seem to extract this information into a relevant class and then use it. Can anyone advise.

    Here is the sample JSON :
    Code:
            {"buckets":{"_total":10,"values":[{"code":"us:0","count":6654,"name":"United States","selected":false},{"code":"gb:0","count":2251,"name":"United Kingdom","selected":false},{"code":"nl:0","count":1055,"name":"Netherlands","selected":false},{"code":"in:0","count":989,"name":"India","selected":false},{"code":"ca:0","count":756,"name":"Canada","selected":false},{"code":"au:0","count":668,"name":"Australia","selected":false},{"code":"us:70","count":621,"name":"Greater New York City Area","selected":false},{"code":"us:84","count":489,"name":"San Francisco Bay Area","selected":false},{"code":"br:0","count":482,"name":"Brazil","selected":false},{"code":"gb:4573","count":397,"name":"London, United Kingdom","selected":false}]},"code":"location","name":"Location"},{"buckets":{"_total":10,"values":[{"code":"96","count":10995,"name":"Information Technology and Services","selected":false},{"code":"4","count":4143,"name":"Computer Software","selected":false},{"code":"6","count":686,"name":"Internet","selected":false},{"code":"5","count":680,"name":"Computer Networking","selected":false},{"code":"80","count":467,"name":"Marketing and Advertising","selected":false},{"code":"118","count":406,"name":"Computer & Network Security","selected":false},{"code":"11","count":359,"name":"Management Consulting","selected":false},{"code":"105","count":302,"name":"Professional Training & Coaching","selected":false},{"code":"8","count":290,"name":"Telecommunications","selected":false},{"code":"3","count":268,"name":"Computer Hardware","selected":false}]},"code":"industry","name":"Industry"}]}
    This is ALL of the JSON including the companysearch. I have managed to extract the companies into datagrid, but I need to extract just the facet information from it.
    Code:
         {"companies":{"_count":20,"_start":0,"_total":21140,"values":[{"companyType":{"code":"C","name":"Public Company"},"employeeCountRange":{"code":"I","name":"10001+"},"industry":"Computer Software","logoUrl":"http://media.linkedin.com/mpr/mpr/p/1/000/035/091/106c50c.png","name":"Microsoft","numFollowers":420823,"status":{"code":"OPR","name":"Operating"},"universalName":"microsoft","websiteUrl":"http://www.microsoft.com/"},{"companyType":{"code":"P","name":"Privately Held"},"employeeCountRange":{"code":"D","name":"51-200"},"industry":"Staffing and Recruiting","logoUrl":"http://media.linkedin.com/mpr/mpr/p/3/000/084/14a/2c2c32e.png","name":"Nigel Frank International","numFollowers":1422,"status":{"code":"OPR","name":"Operating"},"universalName":"nigel-frank-international","websiteUrl":"www.nigelfrank.com"},{"companyType":{"code":"P","name":"Privately Held"},"employeeCountRange":{"code":"E","name":"201-500"},"industry":"Information Technology and Services","logoUrl":"http://media.linkedin.com/mpr/mpr/p/3/000/095/1dc/25b734f.png","name":"TSG","numFollowers":853,"status":{"code":"OPR","name":"Operating"},"universalName":"tsg","websiteUrl":"www.tsg.com"},{"companyType":{"code":"P","name":"Privately Held"},"employeeCountRange":{"code":"E","name":"201-500"},"industry":"Information Technology and Services","logoUrl":"http://media.linkedin.com/mpr/mpr/p/1/000/0d4/29c/2c9f051.png","name":"Outsourcery","numFollowers":561,"status":{"code":"OPR","name":"Operating"},"universalName":"outsourcery","websiteUrl":"http://www.outsourcery.co.uk"},{"companyType":{"code":"P","name":"Privately Held"},"employeeCountRange":{"code":"I","name":"10001+"},"industry":"Information Technology and Services","logoUrl":"http://media.linkedin.com/mpr/mpr/p/3/000/0b3/0d1/3f2b62d.png","name":"Avanade","numFollowers":12252,"status":{"code":"OPS","name":"Operating Subsidiary"},"universalName":"avanade","websiteUrl":"http://www.avanade.com"},{"companyType":{"code":"C","name":"Public Company"},"employeeCountRange":{"code":"G","name":"1001-5000"},"industry":"Information Technology and Services","logoUrl":"http://media.linkedin.com/mpr/mpr/p/3/000/107/2a9/181db5b.png","name":"Columbus Global","numFollowers":1892,"status":{"code":"OPR","name":"Operating"},"universalName":"columbus-global","websiteUrl":"www.columbusglobal.com"},{"companyType":{"code":"P","name":"Privately Held"},"employeeCountRange":{"code":"G","name":"1001-5000"},"industry":"Information Technology and Services","logoUrl":"http://media.linkedin.com/mpr/mpr/p/2/000/01a/3dd/3259d5e.png","name":"Tectura","numFollowers":2116,"status":{"code":"OPR","name":"Operating"},"universalName":"tectura","websiteUrl":"http://www.tectura.com/"},{"companyType":{"code":"C","name":"Public Company"},"employeeCountRange":{"code":"E","name":"201-500"},"industry":"Information Technology and Services","logoUrl":"http://media.linkedin.com/mpr/mpr/p/2/000/050/253/0d6c3d6.png","name":"K3 Retail","numFollowers":345,"status":{"code":"OPR","name":"Operating"},"universalName":"k3-retail","websiteUrl":"http://www.theretailpeople.com"},{"companyType":{"code":"C","name":"Public Company"},"employeeCountRange":{"code":"E","name":"201-500"},"industry":"Information Technology and Services","logoUrl":"http://media.linkedin.com/mpr/mpr/p/1/000/041/0b8/1840449.png","name":"CIBER UK","numFollowers":452,"status":{"code":"OPR","name":"Operating"},"universalName":"ciber-uk","websiteUrl":"http://www.ciber.co.uk"},{"companyType":{"code":"C","name":"Public Company"},"employeeCountRange":{"code":"H","name":"5001-10000"},"industry":"Management Consulting","logoUrl":"http://media.linkedin.com/mpr/mpr/p/1/000/01a/1e7/38e7bfc.png","name":"Hitachi Consulting","numFollowers":7110,"status":{"code":"OPR","name":"Operating"},"universalName":"hitachi-consulting","websiteUrl":"www.hitachiconsulting.com"},{"companyType":{"code":"P","name":"Privately Held"},"employeeCountRange":{"code":"D","name":"51-200"},"industry":"Information Technology and Services","logoUrl":"http://media.linkedin.com/mpr/mpr/p/3/000/070/3f3/24917c4.png","name":"eBECS","numFollowers":309,"status":{"code":"OPR","name":"Operating"},"universalName":"ebecs","websiteUrl":"http://www.ebecs.com"},{"companyType":{"code":"C","name":"Public Company"},"employeeCountRange":{"code":"F","name":"501-1000"},"industry":"Information Technology and Services","logoUrl":"http://media.linkedin.com/mpr/mpr/p/3/000/05e/292/02beaa3.png","name":"Qurius","numFollowers":1205,"status":{"code":"OPR","name":"Operating"},"universalName":"qurius","websiteUrl":"www.qurius.com"},{"companyType":{"code":"P","name":"Privately Held"},"employeeCountRange":{"code":"F","name":"501-1000"},"industry":"Information Technology and Services","logoUrl":"http://media.linkedin.com/mpr/mpr/p/2/000/065/3f4/335ab4d.png","name":"Godrej Infotech Ltd.","numFollowers":1657,"status":{"code":"OPS","name":"Operating Subsidiary"},"universalName":"godrej-infotech-ltd","websiteUrl":"www.godrej.com"},{"companyType":{"code":"P","name":"Privately Held"},"employeeCountRange":{"code":"D","name":"51-200"},"industry":"Information Technology and Services","logoUrl":"http://media.linkedin.com/mpr/mpr/p/2/000/052/240/15b6903.png","name":"Pythagoras","numFollowers":165,"status":{"code":"OPR","name":"Operating"},"universalName":"pythagoras","websiteUrl":"http://www.pythagoras.co.uk"},{"companyType":{"code":"P","name":"Privately Held"},"employeeCountRange":{"code":"D","name":"51-200"},"industry":"Information Technology and Services","logoUrl":"http://media.linkedin.com/mpr/mpr/p/1/000/032/08e/1110a01.png","name":"Concentrix Ltd","numFollowers":209,"status":{"code":"OPR","name":"Operating"},"universalName":"concentrix-ltd","websiteUrl":"www.concentrix.co.uk"},{"companyType":{"code":"C","name":"Public Company"},"employeeCountRange":{"code":"E","name":"201-500"},"industry":"Information Technology and Services","logoUrl":"http://media.linkedin.com/mpr/mpr/p/2/000/051/383/0640d90.png","name":"Maxima","numFollowers":784,"status":{"code":"OPR","name":"Operating"},"universalName":"maxima","websiteUrl":"http://www.maxima.co.uk"},{"companyType":{"code":"C","name":"Public Company"},"employeeCountRange":{"code":"G","name":"1001-5000"},"industry":"Information Technology and Services","logoUrl":"http://media.linkedin.com/mpr/mpr/p/2/000/0b4/123/21e1cf8.png","name":"CSG Limited","numFollowers":1136,"status":{"code":"OPR","name":"Operating"},"universalName":"csg-limited","websiteUrl":"www.csg.com.au"},{"companyType":{"code":"P","name":"Privately Held"},"employeeCountRange":{"code":"G","name":"1001-5000"},"industry":"Computer Software","logoUrl":"http://media.linkedin.com/mpr/mpr/p/1/000/123/0d4/2a5eeee.png","name":"ExactTarget","numFollowers":4111,"status":{"code":"OPR","name":"Operating"},"universalName":"exacttarget","websiteUrl":"http://www.exacttarget.com/"},{"companyType":{"code":"P","name":"Privately Held"},"employeeCountRange":{"code":"D","name":"51-200"},"industry":"Computer Software","logoUrl":"http://media.linkedin.com/mpr/mpr/p/2/000/04c/11c/14c08fb.png","name":"Touchstone Group Plc.","numFollowers":196,"status":{"code":"OPR","name":"Operating"},"universalName":"touchstone-group-plc.","websiteUrl":"www.touchstone.co.uk"},{"companyType":{"code":"P","name":"Privately Held"},"employeeCountRange":{"code":"E","name":"201-500"},"industry":"Computer Software","logoUrl":"http://media.linkedin.com/mpr/mpr/p/3/000/065/133/0afb4ed.png","name":"AlfaPeople","numFollowers":507,"status":{"code":"OPR","name":"Operating"},"universalName":"alfapeople","websiteUrl":"http://www.alfapeople.com/Pages/alfapeople.aspx"}]},"facets":{"_total":2,"values":[{"buckets":{"_total":10,"values":[{"code":"us:0","count":6654,"name":"United States","selected":false},{"code":"gb:0","count":2252,"name":"United Kingdom","selected":false},{"code":"nl:0","count":1055,"name":"Netherlands","selected":false},{"code":"in:0","count":989,"name":"India","selected":false},{"code":"ca:0","count":756,"name":"Canada","selected":false},{"code":"au:0","count":668,"name":"Australia","selected":false},{"code":"us:70","count":621,"name":"Greater New York City Area","selected":false},{"code":"us:84","count":489,"name":"San Francisco Bay Area","selected":false},{"code":"br:0","count":482,"name":"Brazil","selected":false},{"code":"gb:4573","count":398,"name":"London, United Kingdom","selected":false}]},"code":"location","name":"Location"},{"buckets":{"_total":10,"values":[{"code":"96","count":10996,"name":"Information Technology and Services","selected":false},{"code":"4","count":4143,"name":"Computer Software","selected":false},{"code":"6","count":686,"name":"Internet","selected":false},{"code":"5","count":680,"name":"Computer Networking","selected":false},{"code":"80","count":467,"name":"Marketing and Advertising","selected":false},{"code":"118","count":406,"name":"Computer & Network Security","selected":false},{"code":"11","count":359,"name":"Management Consulting","selected":false},{"code":"105","count":302,"name":"Professional Training & Coaching","selected":false},{"code":"8","count":290,"name":"Telecommunications","selected":false},{"code":"3","count":268,"name":"Computer Hardware","selected":false}]},"code":"industry","name":"Industry"}]}}
    Here is the Class generated by Json2Csharp online tool :
    Code:
        public class Value
        {
        public string code { get; set; }
        public int count { get; set; }
        public string name { get; set; }
        public bool selected { get; set; }
        }
    
        public class Buckets
        {
        public int _total { get; set; }
        public List<Value> values { get; set; }
        }
    
        public class RootObject
        {
        public Buckets buckets { get; set; }
        public string code { get; set; }
        public string name { get; set; }
        }
    Here is the C# code which according to Debug is the Buckets info from JSON (there are 2 buckets in the object) :
    Code:
        JObject CompanySearch = JObject.Parse(data);
        IList<JToken> BucketResults = CompanySearch["facets"]["values"].Children().ToList();
    Unfortunately, now I'm stuck. I just cant figure out how to get the Buckets and their values out so that I can turn the collection results into a list of checkboxes that drive an observable collection.

    Can anyone help?

    Regards

    Sam Jones
    Last edited by Cimperiali; February 22nd, 2012 at 03:00 PM. Reason: added [Code] [/Code] tags
    Share on Google+

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center