views:

90

answers:

1

I've been banging my head against the wall on this for a while. Hopefully someone can point out something simple I can tweak so I can get moving again.

I'm hooking up to the WHM API, and I absolutely cannot get the response to deserialize from JSON to an object. I've tried using the JavaScriptSerializer, the DataContractJsonSerializer, and the json.net library that's floating around out there. None of them seem to work. The code is trivial:

resp = req.GetResponse();
using (Stream stream = response.GetResponseStream())
  {
      var serializer = new DataContractJsonSerializer(typeof(result));
      return (result)serializer.ReadObject(stream);
  }

Here's the class to hold the data:

[DataContract]
public class result
{

    [DataMember(Name="options")]
    public Dictionary<string, string> options { get; set; }
    [DataMember(Name="rawout")]
    public string rawout { get; set; }
    [DataMember(Name="status")]
    public bool status { get; set; }
    [DataMember(Name="statusmsg")]
    public string statusmsg { get; set; }
}

And here's the string version of what's coming back over the wire in the response. It's ugly because there's a giant HTML block in the middle of it:

"{\"result\":[{\"status\":1,\"statusmsg\":\"Account Creation Ok\",\"rawout\":\"<table style=\\\"border-bottom: 1px #ccc dotted;\\\"><tr><td width=\\\"100%\\\"><pre>Checking input data...System has 2 free ips.\\n...Done</pre></td><td width=\\\"30\\\"><img align=absmiddle src=\\\"/cPanel_magic_revision_1266578918/cjt/images/icons/success.png\\\"></td></tr></table>\\n<table style=\\\"border-bottom: 1px #ccc dotted;\\\"><tr><td width=\\\"100%\\\"><pre>WWWAcct 12.5.0 (c) 2010 cPanel, Inc....\\n\\nDns Zone check is enabled.\\n+===================================+\\n| New Account Info                  |\\n+===================================+\\n| Domain: aaaaaaaa.com\\n| Ip: x.x.x.x (n)\\n| HasCgi: y\\n| UserName: aaaaaaaa\\n| PassWord: SkIs8_slgTbJPW\\n| CpanelMod: x3\\n| HomeRoot: /home\\n| Quota: 0 Meg\\n| NameServer1: x.com\\n| NameServer2: x.com\\n| NameServer3: \\n| NameServer4: \\n| Contact Email: \\n| Package: default\\n| Feature List: default\\n| Language: en\\n+===
================================+\\n...Done</pre></td><td width=\\\"30\\\"><img align=absmiddle src=\\\"/cPanel_magic_revision_1266578918/cjt/images/icons/success.png\\\"></td></tr></table>\\n<table style=\\\"border-bottom: 1px #ccc dotted;\\\"><tr><td width=\\\"100%\\\"><pre>Running pre creation script (/scripts/prewwwacct)......Done</pre></td><td width=\\\"30\\\"><img align=absmiddle src=\\\"/cPanel_magic_revision_1266578918/cjt/images/icons/success.png\\\"></td></tr></table>\\n<table style=\\\"border-bottom: 1px #ccc dotted;\\\"><tr><td width=\\\"100%\\\"><pre>Adding User...Removing Shell Access (n)\\n...Done</pre></td><td width=\\\"30\\\"><img align=absmiddle src=\\\"/cPanel_magic_revision_1266578918/cjt/images/icons/success.png\\\"></td></tr></table>\\n<table style=\\\"border-bottom: 1px #ccc dotted;\\\"><tr><td width=\\\"100%\\\"><pre>Copying skel files from /home/teamroy/cpanel3-skel/ to /home/aaaaaaaa/......Done</pre></td><td width=\\\"30\\\"><img align=absmiddle src=\\\"/cPanel_magic_revision_12665789
18/cjt/images/icons/success.png\\\"></td></tr></table>\\n<table style=\\\"border-bottom: 1px #ccc dotted;\\\"><tr><td width=\\\"100%\\\"><pre>Adding Entries to httpd.conf......Done</pre></td><td width=\\\"30\\\"><img align=absmiddle src=\\\"/cPanel_magic_revision_1266578918/cjt/images/icons/success.png\\\"></td></tr></table>\\n<table style=\\\"border-bottom: 1px #ccc dotted;\\\"><tr><td width=\\\"100%\\\"><pre>Setting up Mail & Local Domains...localdomains...valiases ...vdomainaliases...vfilters......Done</pre></td><td width=\\\"30\\\"><img align=absmiddle src=\\\"/cPanel_magic_revision_1266578918/cjt/images/icons/success.png\\\"></td></tr></table>\\n<table style=\\\"border-bottom: 1px #ccc dotted;\\\"><tr><td width=\\\"100%\\\"><pre>Configuring DNS...Bind reconfiguring on fit using rndc\\n...Done</pre></td><td width=\\\"30\\\"><img align=absmiddle src=\\\"/cPanel_magic_revision_1266578918/cjt/images/icons/success.png\\\"></td></tr></table>\\n<table style=\\\"border-bottom: 1px #ccc dotted;\\\"><tr><td width=\
\\"100%\\\"><pre>Restarting apache......Done</pre></td><td width=\\\"30\\\"><img align=absmiddle src=\\\"/cPanel_magic_revision_1266578918/cjt/images/icons/success.png\\\"></td></tr></table>\\nChanging password for aaaaaaaa\\nPassword for aaaaaaaa has been changed\\n<table style=\\\"border-bottom: 1px #ccc dotted;\\\"><tr><td width=\\\"100%\\\"><pre>Updating Authentication Databases...Updating ftp passwords for aaaaaaaa\\nFtp password files updated.\\nFtp vhost passwords synced\\n...Done</pre></td><td width=\\\"30\\\"><img align=absmiddle src=\\\"/cPanel_magic_revision_1266578918/cjt/images/icons/success.png\\\"></td></tr></table>\\n<table style=\\\"border-bottom: 1px #ccc dotted;\\\"><tr><td width=\\\"100%\\\"><pre>Verifying MX Records and Setting up Databases...Reconfiguring Mail Routing:\\n<ul><li>LOCAL MAIL EXCHANGER: This server will serve as a primary mail exchanger for aaaaaaaa.com's mail.:<br /> This configuration has been manually selected.<br /><br /></li></ul>...Done</pre></td><td width=\\\"30\\\"><
img align=absmiddle src=\\\"/cPanel_magic_revision_1266578918/cjt/images/icons/success.png\\\"></td></tr></table>\\n<table style=\\\"border-bottom: 1px #ccc dotted;\\\"><tr><td width=\\\"100%\\\"><pre>Setting up Proxy Subdomains......Done</pre></td><td width=\\\"30\\\"><img align=absmiddle src=\\\"/cPanel_magic_revision_1266578918/cjt/images/icons/success.png\\\"></td></tr></table>\\nBind reloading on fit using rndc zone: [aaaaaaaa.com]\\n<table style=\\\"border-bottom: 1px #ccc dotted;\\\"><tr><td width=\\\"100%\\\"><pre>Sending Account Information......Done</pre></td><td width=\\\"30\\\"><img align=absmiddle src=\\\"/cPanel_magic_revision_1266578918/cjt/images/icons/success.png\\\"></td></tr></table>\\nSystem has 2 free ips.\\n<table style=\\\"border-bottom: 1px #ccc dotted;\\\"><tr><td width=\\\"100%\\\"><pre>Running post creation scripts (/scripts/legacypostwwwacct, /scripts/postwwwacct, /scripts/postwwwacctuser)...\\nRunning postwwwacct...Checking Domain aaaaaaaa.com against domain blacklist\\n...Done</pr
e></td><td width=\\\"30\\\"><img align=absmiddle src=\\\"/cPanel_magic_revision_1266578918/cjt/images/icons/success.png\\\"></td></tr></table>\\nwwwacct creation finished\\n<table style=\\\"border-bottom: 1px #ccc dotted;\\\"><tr><td width=\\\"100%\\\"><pre>Setting up Domain Pointers......Done</pre></td><td width=\\\"30\\\"><img align=absmiddle src=\\\"/cPanel_magic_revision_1266578918/cjt/images/icons/success.png\\\"></td></tr></table>\\n<table style=\\\"border-bottom: 1px #ccc dotted;\\\"><tr><td width=\\\"100%\\\"><pre>Setting Reseller Privs......Done</pre></td><td width=\\\"30\\\"><img align=absmiddle src=\\\"/cPanel_magic_revision_1266578918/cjt/images/icons/success.png\\\"></td></tr></table>\\n<table style=\\\"border-bottom: 1px #ccc dotted;\\\"><tr><td width=\\\"100%\\\"><pre>Account Creation Complete!!!...Account Creation Ok...Done</pre></td><td width=\\\"30\\\"><img align=absmiddle src=\\\"/cPanel_magic_revision_1266578918/cjt/images/icons/success.png\\\"></td></tr></table>\\n\",\"options\":{\"nameser
ver4\":null,\"nameserver\":\"x.com\",\"nameserverentry2\":null,\"nameserverentry3\":null,\"nameserverentry4\":null,\"nameserverentry\":null,\"ip\":\"x.x.x.x\",\"nameservera2\":null,\"nameservera3\":null,\"package\":\"default\",\"nameservera4\":null,\"nameserver2\":\"x.com\",\"nameservera\":null,\"nameserver3\":null}}]}"

Edit by Jacob:

Here's a simplified representation of the JSON.

{
    "result":[
        {
            "status":1,
            "statusmsg":"Account Creation Ok",
            "rawout":"[html content]",
            "options":{
                "nameserver4":null,
                "nameserver":"x.com",
                "nameserverentry2":null,
                "nameserverentry3":null,
                "nameserverentry4":null,
                "nameserverentry":null,
                "ip":"x.x.x.x",
                "nameservera2":null,
                "nameservera3":null,
                "package":"default",
                "nameservera4":null,
                "nameserver2":"x.com",
                "nameservera":null,
                "nameserver3":null
            }
        }
    ]
}
+1  A: 

One problem that I can see is that status property is declared as bool, whereas it is numeric in JSON. Try to change it to int.

Also even if conversion succeeds, it will fail at runtime. The stream contains an object that contains an array of results. You are trying to cast it to result. Let me demonstrate. The simplified JSON is something like this:

{
  result: [
    {options:{option1: value}, rawout: "HTML", statusmsg: "message", status: "status"}
  ]
}

The C# object that it would translate to is something like this:

new object {
  result = new IEnumerable<result> {new result{/*properties*/}}
};

So, depending on which library you use, you need to deserialise JSON, grab the value pointed to by result and grab the first element out of that. That would be what you are after.

Igor Zevaka
Ok, I see what you're saying. The weird thing is though that it's not failing - it's just giving me an object with none of the properties set. I was hoping for an exception so I could have something to work on, but with 0 feedback it's hard to fix the problem.
Jim