JsonConvert.DeserializeAnonymousType定义语法问题

问题描述 投票:0回答:1

我有以下代码:

var definition = new { result = "", accountinformation = new[] { "" ,  "" , "" } };

var accountInformationResult = JsonConvert.DeserializeAnonymousType(responseBody, definition);

帐户信息结构作为数组从端点返回,每个元素是包含3个字符串的另一个数组。因此嵌入式阵列不是键值对格式。通过上面的定义,accountinformation返回null。该结构的语法应该是什么?

作为参考,这是PHP端点正在发生的事情。

$account_information[] = array( $billing_company, $customer_account_number, $customer_account_manager );

第一行是循环。因此,多维数组。

echo json_encode(array('result'=>$result, 'account_information'=>$account_information));

我知道我可以使用动态,但为什么要额外的努力?

c# arrays definition jsonconvert
1个回答
0
投票

我假设你的json看起来像这样:

{
  "result": "the result",
  "account_information": [
    ["company1", "account_number1", "account_manager1"],
    ["company2", "account_number2", "account_manager2"]
  ]
}

在这种情况下,您应该能够使用以下定义反序列化(请注意account_information中的下划线:

var definition = new { result = "", account_information = new List<string[]>() };

在json中,您可以在数据模型更改时随意添加额外的属性。因此,如果您定义的数据模型不包含其中一个属性,则可以简单地忽略该属性。在您的情况下,定义没有名为account_information(确切)的属性,因此在反序列化时忽略json的这一部分。

编辑:如果它仍然是一个匿名的abject,你也可以考虑解析为JObject

var obj = JObject.Parse(responseBody);
string firstCompany = obj["account_information"][0][0];
string secondCompany = obj["account_information"][1][0];
© www.soinside.com 2019 - 2024. All rights reserved.