如何将JSON结果解析为动态对象
本文关键字:动态 对象 结果 JSON | 更新日期: 2023-09-27 17:58:23
我正在尝试编写一个C#实用程序来使用MailChimp从导出API返回的结果。
文档指出,结果将以"流式JSON"的形式返回
"这意味着对此API的调用将不会返回单个有效的JSON对象,而是一系列由换行符。"
我看到的结果在我看来不像普通的JSON,也不是我所期望的。在我看来,它就像是用方括号包裹的CSV数据,第一行是行标题。
可以在这里查看一些结果。我也会把它们贴在下面。
["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#对象的最佳方法是什么。
EDIT:我已经确认数据流是有效的JSON,使用http://www.freeformatter.com/json-validator.html以及粘贴上面的采样线。因此,我所希望的是一种基于第一行动态创建对象的方法,然后创建这些对象的列表,这些对象的值包含在随后的行中。
你是对的,这不是典型的JSON形式。你可以做的是创建一个Dictionary<string, string>
对象的集合。使用响应的第一部分作为字典的键,然后使用在结果的后续部分中找到的值作为每个字典的值。