0

I have the following code:

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

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

The account information structure comes back from an endpoint as an array with each element being another array containing 3 strings. So the embedded array is not in a key value pair format. With the above definition accountinformation returns null. What should the syntax be for this structure?

For reference this is what is going on in the php endpoint.

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

This first line is in a loop. Hence the multi-dimensional array.

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

I know I could use dynamic but why the extra effort?

4

1 回答 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 的这一部分。

编辑:如果无论如何它都会是一个匿名的下贱,你也可以考虑解析成JObject

var obj = JObject.Parse(responseBody);
string firstCompany = obj["account_information"][0][0];
string secondCompany = obj["account_information"][1][0];
于 2019-03-19T12:18:19.673 回答