Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Consuming Json Object RSS

7 replies

Last post Jun 20, 2008 03:22 PM by hari4u

(0)
  • hari4u

    hari4u

    Member

    58 Points

    94 Posts

    Consuming Json Object

    Jun 17, 2008 02:05 PM | LINK

     Hi,

    Could anyone help me with this in detail,

    i got a json response need to get the content out of the json

    below is my reponse.....

    {"Body":{"AuthResponse":{"skin":[{"_content":"beach"}],"sessionId":{"_content":"162","id":"162"},"lifetime":172800000,"authToken":"0_fcf6732b7e4fcbb7bef9e4beab1c9164c9535aa7_69643d33363a65343438663963382d343639332d343864322d623531652d6137346438353836663863663b6578703d31333a313231333836313430363030373b747970653d363a7a696d6272613b6d61696c686f73743d31343a3139322e3136382e312e373a38303b","attrs":{"_attrs":{"displayName":"hari prasad"}},"_jsns":"urn:zimbraAccount"}},"Header":{"context":{"sessionId":[{"_content":"162","id":"162"}],"change":{"token":286},"refresh":{"mbx":[{"s":0}],"tags":{},"folder":[{"n":0,"l":"11","folder":[{"view":"document","n":0,"l":"1","name":"Briefcase","s":0,"id":"16","rev":1},{"view":"appointment","f":"#","n":0,"l":"1","name":"Calendar","s":0,"id":"10","rev":1},{"view":"message","n":0,"l":"1","name":"Chats","s":0,"id":"14","rev":1},{"view":"contact","n":0,"l":"1","name":"Contacts","s":0,"id":"7","rev":1},{"view":"message","n":0,"l":"1","name":"Drafts","s":0,"id":"6","rev":1},{"view":"contact","n":0,"l":"1","name":"Emailed Contacts","s":0,"id":"13","rev":1},{"view":"message","n":0,"l":"1","name":"Inbox","s":0,"id":"2","rev":1},{"view":"message","n":0,"l":"1","name":"Junk","s":0,"id":"4","rev":1},{"view":"wiki","n":0,"l":"1","name":"Notebook","s":0,"id":"12","rev":1},{"view":"message","n":0,"l":"1","name":"Sent","s":0,"id":"5","rev":1},{"view":"task","f":"#","n":0,"l":"1","name":"Tasks","s":0,"id":"15","rev":1},{"n":0,"l":"1","name":"Trash","s":0,"id":"3","rev":1}],"name":"USER_ROOT","s":0,"id":"1","rev":1}],"version":"5.0.6_GA_2313.RHEL5_64 20080522130456 20080522-1307 FOSS"},"_jsns":"urn:zimbra"}},"_jsns":"urn:zimbraSoap"}

    thanx in advance,

    Hari. 

     

  • chandler.chao

    chandler.chao

    Member

    307 Points

    87 Posts

    Re: Consuming Json Object

    Jun 17, 2008 09:15 PM | LINK

    Take a look at the DataContractJsonSerializer object...works like a charm from what I remember, however I do recall that there is something funky that can throw it off, just can't remember what that was specifically.  I would like to say it was when you serialize an empty string but I really can't remember right now.

    Chandler Chao
    iStreamPlanet
  • hari4u

    hari4u

    Member

    58 Points

    94 Posts

    Re: Re: Consuming Json Object

    Jun 18, 2008 06:33 AM | LINK

    thanx for the reply,

    but am little newbie , can anybody do this in detail for me.

    thanx

    hari 

  • Yi-Lun Luo - MSFT

    Yi-Lun Luo -...

    All-Star

    25149 Points

    2759 Posts

    Microsoft

    Re: Consuming Json Object

    Jun 19, 2008 09:20 AM | LINK

    Hello, first you must add a reference to System.Json.dll and System.ServiceModel.Web assembly.

    Now you need to create some classes based on your JSON content. For example, let's say you want to get information about the folders. You can create a Folder class:

    public class Folder
    {
    public string View { get; set; }
    public string F { get; set; }
    public int N { get; set; }
    public string L { get; set; }
    public string Name { get; set; }
    public int S { get; set; }
    public string Id { get; set; }
    public int Rev { get; set; }
    }

    Now you have two options. One is to use DataContractJsonSerializer, and the other is to use LINQ to JSON. For such complex JSON content, LINQ to JSON can be a better choice.

    //json is a string that contains you JSON content.
    JsonObject root = (JsonObject)JsonObject.Parse(json);
    //Navigate to the folder array.
    JsonArray folders = (JsonArray)root["Header"]["context"]["refresh"]["folder"][0]["folder"];
    List<Folder> result = new List<Folder>();
    //For each folder in the array, create a new Folder object.
    foreach (JsonObject jo in folders)
    {
    Folder folder = new Folder();
    if (jo.Keys.Contains("view"))
    {
    folder.View = jo["view"];
    }
    if (jo.Keys.Contains("f"))
    {
    folder.F = jo["f"];
    }
    if (jo.Keys.Contains("n"))
    {
    folder.N = jo["n"];
    }
    if (jo.Keys.Contains("l"))
    {
    folder.L = jo["l"];
    }
    if (jo.Keys.Contains("name"))
    {
    folder.Name = jo["name"];
    }
    if (jo.Keys.Contains("s"))
    {
    folder.S = jo["s"];
    }
    if (jo.Keys.Contains("id"))
    {
    folder.Id = jo["id"];
    }
    if (jo.Keys.Contains("rev"))
    {
    folder.Rev = jo["rev"];
    }
    result.Add(folder);
    }

    shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
  • Allen Chen – MSFT

    Allen Chen –...

    Star

    13909 Points

    1810 Posts

    Microsoft

    Re: Consuming Json Object

    Jun 19, 2008 09:21 AM | LINK

     

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • hari4u

    hari4u

    Member

    58 Points

    94 Posts

    Re: Re: Consuming Json Object

    Jun 19, 2008 02:43 PM | LINK

     thanx a lot sir,

    but am assigning them to listbox am getting the output as

    Zimbra.Folder

    can u help me with this ...

    Hari 

  • lee_sl

    lee_sl

    Contributor

    4222 Points

    864 Posts

    Re: Re: Re: Consuming Json Object

    Jun 19, 2008 03:17 PM | LINK

    you have to specify itemtemplate and display the content you want to see for each item

    ----------------------------------------------
    Available for consulting in Dallas, TX
    http://leeontech.wordpress.com/
  • hari4u

    hari4u

    Member

    58 Points

    94 Posts

    Re: Re: Re: Re: Consuming Json Object

    Jun 20, 2008 03:22 PM | LINK

     hi,

    i have to parse the body part , 

    {"Body":{"AuthResponse":{"skin":[{"_content":"beach"}],"sessionId":{"_content":"162","id":"162"},"lifetime":172800000,"authToken":"0_fcf6732b7e4fcbb7bef9e4beab1c9164c9535aa7_69643d33363a65343438663963382d343639332d343864322d623531652d6137346438353836663863663b6578703d31333a313231333836313430363030373b747970653d363a7a696d6272613b6d61696c686f73743d31343a3139322e3136382e312e373a38303b","attrs":{"_attrs":{"displayName":"hari prasad"}},"_jsns":"urn:zimbraAccount"}}

    i need display name attribute,

    have a class defined for it as

    public class Attrs
        {
             public string a{get;set;}
             public string displayname { get; set; }
        }

    JsonObject root = (JsonObject)JsonObject.Parse(responseString);
                JsonArray k= (JsonArray)root["Body"]["AuthResponse"]["attrs"][0]["_attrs"];
                List<Attrs> result = new List<Attrs>();
                //For each folder in the array, create a new Folder object.
                foreach (JsonObject jo in k)
                {
                    Attrs s = new Attrs();
                    if (jo.Keys.Contains("displayname"))
                    {
                       s.displayname = jo["displayname"];
                    }

                 result.add(s);



    but am failing near jsonarray k where it is givin an error as method not supported ,

    could you please tell me where i did wrong

    thanx in advance,

    Hari.