如何将JSON结果解析为动态对象

时间:2014-04-15 18:38:03

标签: c# json parsing mailchimp restsharp

我正在尝试编写一个C#实用程序来使用从Export API by MailChimp返回的结果。

文档说明结果将作为“流式JSON”返回。

  

“这意味着对此API的调用不会返回单个有效的JSON   对象,但是,由一系列有效的JSON对象分隔   换行符。“

我看到的结果对我来说看起来不像普通的JSON,并且不是我期望的那样。它看起来像是用方括号括起来的CSV数据,第一行有行标题。

结果can be viewed here的剪辑。我也将它们粘贴在下面。

["Email Address","First Name","Last Name","Company","FirstOrder","LastOrder","CustomerID","SalesRep","ScreenName","PlayerPage","PlayerPDF","Services Purchased","Contests","EMAIL_TYPE","MEMBER_RATING","OPTIN_TIME","OPTIN_IP","CONFIRM_TIME","CONFIRM_IP","LATITUDE","LONGITUDE","GMTOFF","DSTOFF","TIMEZONE","CC","REGION","LAST_CHANGED","LEID","EUID"]
["john@domain.com","John","Doe","ACME Inc","2010-09-07","2010-09-07","ABC123","sally","","","","Service1","","html",2,"",null,"2011-12-23 15:58:44","10.0.1.1","34.0257000","-84.1418000","-5","-4","America\/Kentucky\/Monticello","US","GA","2014-04-11 18:38:39","40830325","82c81e14a"]
["jane@domain2.com","Jane","Doe","XYZ Inc","2011-05-02","2011-05-02","XYZ001","jack","","","","Service2","","html",2,"",null,"2011-12-23 15:58:44","10.0.1.1","34.0257000","-84.1418000","-5","-4","America\/Kentucky\/Monticello","US","GA","2014-04-11 18:38:40","40205835","6c23329a"]

你能帮助我理解返回的内容 - 因为它似乎不是正常的JSON。什么是我将这个数据流解析为C#对象的最佳方法。

编辑:我已使用http://www.freeformatter.com/json-validator.html确认数据流是有效的JSON并粘贴在上面的示例行中。所以我希望的是一种基于第一行动态创建对象的方法,然后使用后续行中包含的值创建这些对象的列表。

1 个答案:

答案 0 :(得分:0)

你是对的,这不是典型的JSON形式。你可以做的是创建一个Dictionary<string, string>个对象的集合。使用响应的第一部分作为字典的键,然后将结果的后续部分中的值用作每个字典的值。