3

我有一个从中接收数据的 API。该 API 无法控制它的结构,我需要序列化和反序列化 JSON 输出以将数据映射到我的模型。

在 JSON 使用命名属性很好地格式化的情况下,一切都很好。

如果没有命名值并且只有一个整数和字符串数组,你能做什么?就像在位置下

这是 JSON 的示例:

{"id":"2160336","activation_date":"2013-08-01","expiration_date":"2013-08-29","title":"Practice Manager","locations":{"103":"Cambridge","107":"London"}}

我有类似的模型:

public class ItemResults
{
    public int Id { get; set; }

    public DateTime Activation_Date { get; set; }

    public DateTime Expiration_Date{ get; set; } 

    public string Title { get; set; }

    public Location Locations { get; set; }
}

public class Location
{
    public int Id { get; set; }

    public string value { get; set; }
}

我正在使用内置的 ajax 序列化进行映射:

 protected T MapRawApiResponseTo<T>( string response )
    {
        if ( string.IsNullOrEmpty( response ) )
        {
            return default( T );
        }

        var serialize = new JavaScriptSerializer();

        return serialize.Deserialize<T>( response );
    }

var results = MapRawApiResponseTo<ItemResults>(rawApiResponse);

因此,ID 和所有其他属性都被拾取和映射,但我所做的每件事似乎都无法映射位置。

非常感谢

4

4 回答 4

2

如果你不介意,你可以用字典解决:

class Program
{
    static void Main(string[] args)
    {
        string json =
            "{'id':'2160336','activation_date':'2013-08-01','expiration_date':'2013-08-29','title':'Practice Manager','locations':{'103':'Cambridge','107':'London'}}";
        var deserializeObject = JsonConvert.DeserializeObject<ItemResults>(json);
        Console.WriteLine("{0}:{1}", deserializeObject.Locations.First().Key, deserializeObject.Locations.First().Value);
        Console.ReadKey();
    }
}

public class ItemResults
{
    public int Id { get; set; }
    public DateTime Activation_Date { get; set; }
    public DateTime Expiration_Date { get; set; }
    public string Title { get; set; }
    public Dictionary<int, string> Locations { get; set; }
}

您也可以使用手动解析,例如:Json.NET (Newtonsoft.Json) - 两个同名的“属性”?

于 2013-08-14T13:34:19.853 回答
2
public Dictionary<int,string> Locations { get; set; }

job done; you should find that using Json.NET, at least, i.e.

var result = JsonConvert.DeserializeObject<ItemResults>(json);

you get 2 entries in result.Locations; specifically result[103] = "Cambridge"; and result[107] = "London";

于 2013-08-14T13:33:54.813 回答
1

我向您建议以下解决方案:

public class ItemResults
{
    public int Id { get; set; }

    public DateTime Activation_Date { get; set; }

    public DateTime Expiration_Date { get; set; }

    public string Title { get; set; }

    [JsonProperty("locations")]
    public JObject JsonLocations { get; set; }

    [JsonIgnore]
    public List<Location> Locations { get; set; }

    [OnDeserialized]
    public void OnDeserializedMethod(StreamingContext context)
    {
        this.Locations = new List<Location>();
        foreach (KeyValuePair<string, JToken> item in this.JsonLocations)
        {
            this.Locations.Add(new Location() { Id = int.Parse(item.Key), value = item.Value.ToString() });
        }
    }
}

public class Location
{
    public int Id { get; set; }

    public string value { get; set; }
}

在你只需要反序列化你的 JSON 之后:JsonConvert.DeserializeObject<ItemResults>(json);

于 2013-08-14T13:41:03.257 回答
1

这将起作用:

public Dictionary<string, string> Locations { get; set; }

public IEnumerable<Location> LocationObjects { get { return Locations
     .Select(x => new Location { Id = int.Parse(x.Key), value = x.Value }); } }
于 2013-08-14T13:37:18.713 回答