1

下面是一个 Json :

[{
    "Result": {
        "description": "Application Security Supp Specialist",
        "code": "40000003"
    }
}, {
    "Result": {
        "description": "Gvt Cyber Intelligence Specialist",
        "code": "40001416"
    }
}, {
    "Result": {
        "description": "Gvt Record Retention Specialist",
        "code": "40001428"
    }
}]

下面是我创建的类结构,因为我需要将其填充到 C# 对象中。我正在尝试创建RulesEngineOutput的集合并用 json 内容填充它。

public class RulesEngineOutput
{
    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("code")]
    public string Code { get; set; }
}

public class RulesEngineOutputCollection
{
    public IEnumerable<RulesEngineOutput> ProbableRoles { get; set; }
}

我正在尝试使用以下代码来实现这一点:

var bodyJson = JsonConvert.SerializeObject(bodyString);
RulesEngineOutputCollection result = new RulesEngineOutputCollection();
foreach (var item in bodyJson)
{
    result = JsonConvert.DeserializeObject<RulesEngineOutputCollection>(item.ToString()); 
}

但这会引发异常,因为该项目得到一个字符,我认为我需要在循环中传递一个 JSON 对象,但我无法得到一个。每次我得到的是一个 JSON 字符串。

无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“RulesEngineOutputCollection”,因为该类型需要 JSON 对象(例如 {\"name\":\"value\"})才能正确反序列化。\r \n要修复此错误,请将 JSON 更改为 JSON 对象(例如 {\"name\":\"value\"})或将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection、IList ) 比如可以从 JSON 数组反序列化的 List。

4

3 回答 3

1

问题是您RulesEngineOutput和您的收藏之间有一个中介对象。您需要像这样重组对象:

public class RulesEngineOutput
{
    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("code")]
    public string Code { get; set; }
}

public class RulesEngineOutputResult
{
    public RulesEngineOutput Result { get; set; }
}

public class RulesEngineOutputCollection
{
    public IEnumerable<RulesEngineOutputResult> ProbableRoles { get; set; }
}

然后当你完成这个重组后,你可以直接反序列化到你的RulesEngineOutputCollection而不是一个对象,然后再次迭代和反序列化。

result = JsonConvert.DeserializeObject<RulesEngineOutputCollection>(bodyString); 
于 2018-12-10T22:51:46.377 回答
0

非常感谢 Max、Nathan 和其他人。所以最后我对代码进行了一些更改,下面是我为使事情正常进行而更改的代码:

 var jToken = JObject.Parse(responseContent);
        var bodyString = jToken.SelectToken("body");
        var bodyJson = JsonConvert.SerializeObject(bodyString);


        List<RulesEngineOutput> result = new List<RulesEngineOutput>();


        try
        {

            foreach (var item in bodyString)
            {
                var formattedItem = item.SelectToken("Result");
                var  resultItem = JsonConvert.DeserializeObject<RulesEngineOutput>(formattedItem.ToString());
                   result.Add(resultItem);
            }


        }

希望它也对其他人有所帮助。

于 2018-12-11T23:27:41.277 回答
-1

正如 Nathan Werry 所说,您将一个对象分层到另一个对象中,因此,您无法以您想要的方式反序列化数据。但是,如果您首先创建这些结果的数组并稍后将其分配给您的ProbableRoles属性,则可以解决此问题:

var rules = new RulesEngineOutputCollection
{
    ProbableRoles = JsonConvert.DeserializeObject<Result[]>(bodyString).Select(r => r.Data).ToList()
};

public class Result
{
    [JsonProperty("Result")]
    public RulesEngineOutput Data { get; set; }
}

其他一切都保持不变。您基本上从结果数组中创建了一个新列表。我也可以Select()直接分配结果(无需调用.ToList()),但这可以确保对象实际上具有数据,而不仅仅是对枚举的引用。

于 2018-12-10T23:05:49.693 回答