试图反序列化JSON

时间:2010-08-26 22:40:59

标签: c# json serialization

我一直在撞墙上撞了一会儿。希望有人可以指出我可以调整的简单内容,这样我就可以再次行动。

我正在连接到WHM API,我绝对无法获得从JSON反序列化到对象的响应。我已经尝试过使用JavaScriptSerializer,DataContractJsonSerializer和那些漂浮在那里的json.net库。他们似乎都没有工作。代码很简单:

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

这是保存数据的类:

[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; }
}

这是响应中通过线路返回的字符串版本。它很难看,因为它的中间有一个巨大的HTML块:

"{\"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}}]}"

Jacob编辑:

这是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 个答案:

答案 0 :(得分:2)

我可以看到的一个问题是status属性被声明为bool,而它在JSON中是数字。尝试将其更改为int

即使转换成功,它也会在运行时失败。该流包含一个包含result数组的对象。您正试图将其投射到result。让我来证明一下。简化的JSON是这样的:

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

它将转换为的C#对象是这样的:

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

因此,根据您使用的库,您需要反序列化JSON,获取result指向的值并从中获取第一个元素。这就是你想要的。