简体   繁体   English

使用Shopify API将JSON反序列化为C#对象

[英]Deserializing JSON into C# object with Shopify API

I want to convert the JSON output of the Shopify API call into an object I can manipulate in C#. 我想将Shopify API调用的JSON输出转换为可以在C#中操作的对象。

This is the code I have so far 这是我到目前为止的代码

1 var client = new ShopifyAPIClient(new ShopifyAuthorizationState { AccessToken = token, ShopName = shopName });
2 var orders = JsonConvert.DeserializeObject((string)client.Get("/admin/orders.json"));
3 
4 foreach(object order in orderss){
5  // do stuff with member variables of object
6 }

Error on line 4: Type 'object' is not enumerable 第4行错误: 类型'object'不可枚举

this page has the format of the JSON object returned. 页面具有返回的JSON对象的格式。

this page has an example of how to deserialize JSON into a C# object that has a class defined 页面提供了有关如何将JSON反序列化为具有已定义类的C#对象的示例

Is there any way to deserialize the JSON into a C# object that can be enumerable without defining a class? 有什么方法可以将JSON反序列化为C#对象,而无需定义类就可以枚举? If not, are the Shopify classes defined somewhere where I can copy them into my project? 如果不是,是否在可以将它们复制到项目中的某个地方定义了Shopify类?

Thank you in advance! 先感谢您!

it has another orders object under root object. 它在根对象下还有另一个orders对象。 Try 尝试

dynamic dynObj = JsonConvert.DeserializeObject(json);
foreach (var order in dynObj.orders)
{
    Console.WriteLine("{0}  {1}", 
                        (string)order.email, 
                        (string)order.payment_details.credit_card_number);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM