在C#中反序列化3taps JSON数据

时间:2014-07-31 15:04:12

标签: c# .net json api

这是我第一次使用JSON。我在调试时得到一个'null'解析的响应。不确定我是否错误地反序列化JSON或者我的直接传输对象是否错误。请帮忙。

JSON看起来像这样:

{
"success": true,
"anchor": 1257781939,
"postings": [
    {
        "id": 1257767757,
        "external_url": "http://miami.craigslist.org/brw/atq/4591006794.html",
        "heading": "Antique Chairs Two ",
        "body": "\n Antique Chairs Two of them, the seat is 18\" hi $95 for two. call anytime\n show contact info\n ",
        "timestamp": 1406731621,
        "price": 95,
        "images": [
            {
                "full": "http://images.craigslist.org/00404_bc7wiVq6I7O_600x450.jpg"
            }
        ],
        "annotations": {
            "source_continent": "USA",
            "source_cat": "sss",
            "source_neighborhood": "coconut creek",
            "source_state": "Florida",
            "source_loc": "miami",
            "source_subloc": "brw",
            "source_map_google": "https://maps.google.com/maps/preview/@26.253500,-80.177500,16z",
            "source_map_yahoo": "http://maps.yahoo.com/#mvt=m&lat=26.253500&lon=-80.177500&zoom=16",
            "latlong_source": "In Posting",
            "proxy_ip": "186.91.140.57:8080",
            "source_heading": "Antique Chairs Two ",
            "phone": "9545543144",
            "source_account": "dmpsk-4591006794@sale.craigslist.org",
            "original_posting_date": "1406553713",
            "source_subcat": "ata|atq",
            "condition": "new"
        },
        "location": {
            "country": "USA",
            "state": "USA-FL",
            "metro": "USA-MIA",
            "region": "USA-MIA-BRO",
            "county": "USA-FL-BRW",
            "city": "USA-MIA-POM",
            "zipcode": "USA-33066",
            "lat": "26.2535",
            "long": "-80.1775",
            "accuracy": 8,
            "geolocation_status": 3
        }
    }

已更新 我的DTO文件夹中有四个类,名为PostResponse,Postings,Annotations和Location。第一个,PostResponse看起来像这样:

using System;
using System.Collections.Generic;

namespace AutoPoster.Core.DTO
{
    public class PostResponse
    {
        public bool success {get; set;}
        public uint anchor {get; set;}
        public List<Postings> Postings {get; set;}
    }
}

JSON数据很好,但是反序列化时我得到parsedResponse = null。这是我将JSON映射到模型对象的调用。

        if (string.IsNullOrEmpty(jsonResponse))
            return postInfo;

        var parsedResponse = await Task.Run(() => JsonConvert.DeserializeObject<DTO.Postings>(jsonResponse)).ConfigureAwait(false);

        postInfo = await MapDtoToPostingInfo(parsedResponse).ConfigureAwait(false);

        return postInfo;

非常感谢任何帮助。谢谢!

2 个答案:

答案 0 :(得分:1)

问题是你的Postings类只代表返回的响应对象的一部分(即postings数组中的单个对象),它不在根目录。

尝试反序列化代表整个响应对象的类。例如:

public class Response
{
    public bool success {get; set;}
    public uint anchor {get; set;}
    public List<Postings> Postings {get; set;}
}

答案 1 :(得分:1)

当您拥有JSON时,我的建议是使用json2csharp生成正确的类。

public class Image
{
    public string full { get; set; }
}

public class Annotations
{
    public string source_continent { get; set; }
    public string source_cat { get; set; }
    public string source_neighborhood { get; set; }
    public string source_state { get; set; }
    public string source_loc { get; set; }
    public string source_subloc { get; set; }
    public string source_map_google { get; set; }
    public string source_map_yahoo { get; set; }
    public string latlong_source { get; set; }
    public string proxy_ip { get; set; }
    public string source_heading { get; set; }
    public string phone { get; set; }
    public string source_account { get; set; }
    public string original_posting_date { get; set; }
    public string source_subcat { get; set; }
    public string condition { get; set; }
}

public class Location
{
    public string country { get; set; }
    public string state { get; set; }
    public string metro { get; set; }
    public string region { get; set; }
    public string county { get; set; }
    public string city { get; set; }
    public string zipcode { get; set; }
    public string lat { get; set; }
    public string @long { get; set; }
    public int accuracy { get; set; }
    public int geolocation_status { get; set; }
}

public class Posting
{
    public int id { get; set; }
    public string external_url { get; set; }
    public string heading { get; set; }
    public string body { get; set; }
    public int timestamp { get; set; }
    public int price { get; set; }
    public List<Image> images { get; set; }
    public Annotations annotations { get; set; }
    public Location location { get; set; }
}

public class RootObject
{
    public bool success { get; set; }
    public int anchor { get; set; }
    public List<Posting> postings { get; set; }
}

然后,您应该能够反序列化为RootObject。

var rootObject = JsonConvert.DeserializeObject<RootObject>(jsonResponse);