为什么我不能反序列化Json数组C#?

时间:2018-12-14 13:00:38

标签: c# json

我尝试反序列化此Json:JsonStruct

我有阵列图片:JsonStruct

我有这个C#代码:

struct SampleModel: Codable {
    let showId: String
    enum CodingKeys: String, CodingKey {
        case showId
    }

    init(from decoder: Decoder) throws {

        let container = try decoder.container(keyedBy: CodingKeys.self)

        do {

            let id = try container.decode(String.self, forKey: .showId)
            let stored = id == "one" ? "First" : "default"
            self.init(showId:stored)

        } catch {
            print(error)
            throw error
        }
     }
}

当我尝试反序列化此Json时,出现此错误: Error

Json: class KickStarterJson { public List<ProjectInfo> projects { get; set; } public int total_hits { get; set; } public int live_projects_count { get; set; } } class ProjectInfo { public string name { get; set; } public int pledged { get; set; } public string currency { get; set; } }

文本错误:System.FormatException:“输入字符串的格式不正确。”

1 个答案:

答案 0 :(得分:1)

尝试使用以下代码:

var jsonSerializerSettings = new JsonSerializerSettings
{
    MissingMemberHandling = MissingMemberHandling.Ignore
};

var kickStarterJson = JsonConvert.DeserializeObject<KickStarterJson>(jsonResponse, jsonSerializerSettings);

编辑:实际错误是已填充必须是小数,而不是整数。

改为使用此类:

class ProjectInfo
{
    public string name { get; set; }
    public decimal pledged { get; set; } 
    public string currency { get; set; }
}
相关问题