简单的JSON反序列化



我知道这并不难,但我已经盯着JSON.Net文档看了半个小时,不知道它是如何工作的。我已经设置好了,但我最初的尝试失败了,到目前为止,我所看到的文档并没有让我明白(至少对我来说)如何反序列化数组形式的response.data,然后获取每个记录的各个字段。

此外,我不需要使用JSON.Net——它似乎是一个不错的选择。任何解决方案都可以。

{
    "request":{
        "Format":"json",
        "Target":"Affiliate",
        "Method":"findAll",
        "Service":"HasOffers",
        "Version":"2",
        "NetworkId":"directagents",
        "NetworkToken":"......"
    },
    "response":{
        "status":1,
        "data":{
            "2056":{
                "Affiliate":{
                    "id":"2056",
                    "company":
                    "123 Greetings 123 Greetings (CD186)",
                    "address1":"1 Change Address",
                    "address2":"",
                    "city":"City",
                    "region":"NY",
                    "country":"US",
                    "other":null,
                    "zipcode":"11111",
                    "phone":"-",
                    "fax":null,
                    "website":null,
                    "signup_ip":null,
                    "date_added":"2012-02-24 18:00:24",
                    "modified":-62169966000,
                    "ref_id":"CD186",
                    "status":"pending",
                    "wants_alerts":"1",
                    "account_manager_id":"20",
                    "payment_method":"check",
                    "payment_terms":"net 30",
                    "w9_filed":"0",
                    "referral_id":null,
                    "affiliate_tier_id":null,
                    "fraud_activity_score":null,
                    "fraud_activity_alert_threshold":null,
                    "fraud_activity_block_threshold":null,
                    "fraud_profile_alert_threshold":null,
                    "fraud_profile_block_threshold":null,
                    "scrub_offer_pixels":null,
                    "shared":"0"
                    }
                },
            "1606":{
                                   .......this is enough to see how its structured....

您可以将该"数组"建模为Dictionary<int, AffiliateHolder>。JSON的类可能如下所示:

class Top
{
    public Request Request { get; set; }
    public Response Response { get; set; }
}
class Request
{
    public string Format { get;set; }
    // etc.
}
class Response
{
    public int Status { get; set; }
    public Dictionary<int, AffiliateHolder> Data { get; set; }
}
class AffiliateHolder
{
    public Affiliate Affiliate { get; set; }
}
class Affiliate
{
    public int Id { get; set; }
    // etc.
}
var o = JsonConvert.DeserializeObject<Top>(myJSONString);

最快、最简单的方法是使用Json.NET和LINQ to Json。举个例子,下面的代码是:

var parsedJson = JObject.Parse(rawJson);
foreach (var item in parsedJson["response"]["data"])
{
   Console.WriteLine(item.ToString());
}

会在控制台上写下以下内容:

"2056": {
  "Affiliate": {
    "id": "2056",
    "company": "123 Greetings 123 Greetings (CD186)",
    "address1": "1 Change Address",
    "address2": "",
    "city": "City",
    "region": "NY",
    "country": "US",
    "other": null,
    "zipcode": "11111",
    "phone": "-",
    "fax": null,
    "website": null,
    "signup_ip": null,
    "date_added": "2012-02-24 18:00:24",
    "modified": -62169966000,
    "ref_id": "CD186",
    "status": "pending",
    "wants_alerts": "1",
    "account_manager_id": "20",
    "payment_method": "check",
    "payment_terms": "net 30",
    "w9_filed": "0",
    "referral_id": null,
    "affiliate_tier_id": null,
    "fraud_activity_score": null,
    "fraud_activity_alert_threshold": null,
    "fraud_activity_block_threshold": null,
    "fraud_profile_alert_threshold": null,
    "fraud_profile_block_threshold": null,
    "scrub_offer_pixels": null,
    "shared": "0"
  }
}

没有必要为显式反序列化创建具体的类。显然,这是一个非常有效的方法,然而,如果这是你的风格偏好。

请参阅http://james.newtonking.com/projects/json/help/LINQtoJSON.html以获取更多示例。

我认为您可以尝试创建一个简单的示例,而不是像那样创建一个巨大的json。看看http://servicestack.net/它是Json.NET的替代方案,速度更快,学习曲线也很小。你可以在网站上找到很多例子。

这是javascript中的吗?你应该能够做到这一点:

JSON.parse(response.data)

相关内容

  • 没有找到相关文章

最新更新