我使用以下C#代码使用JSON.Net框架将一串JSON数据转换为动态对象:
// Creates a dynamic .Net object representing the JSON data
var ProductDB = JsonConvert.DeserializeObject<dynamic>(JsonData);
Run Code Online (Sandbox Code Playgroud)
转换后,我可以使用以下代码直接访问元素:
// Variables to be used
string ProductID;
string ProductType;
int ProductQty;
// Loop through each of the products
foreach (dynamic product in ProductDB.products)
{
ProductID = product.id;
ProductType = product.type;
ProductQty = product.qty;
}
Run Code Online (Sandbox Code Playgroud)
使用XML数据有什么类似的东西吗?我可以使用JSON.net将我的XML转换为JSON,然后重新使用上面的代码,但这就像是作弊.
谢谢.