简体   繁体   English

如何解析JSON生成动态对象

[英]How to parse JSON results in to a dynamic object

I'm trying to write a C# utility to consume the results returned from the Export API by MailChimp . 我正在尝试编写C#实用程序,以使用MailChimpExport API返回的结果。

The documentation states that the results will be returned as "streamed JSON." 该文档指出结果将作为“流JSON”返回。

"This means that a call to this API will not return a single valid JSON object but, rather, a series of valid JSON objects separated by newline characters." “这意味着对该API的调用不会返回单个有效的JSON对象,而是返回一系列由换行符分隔的有效JSON对象。”

The results that I'm seeing don't look like normal JSON to me, and aren't what I was expecting to be working with. 我看到的结果对我来说看起来不像是普通的JSON,而且也不是我期望的结果。 It looks to me like CSV data wrapped in square brackets, with row headers in the first line. 在我看来,CSV数据包裹在方括号中,第一行中包含行标题。

A snip of the results can be viewed here . 可以在此处查看结果片段。 I'll paste them below as well. 我也将它们粘贴在下面。

["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"]

Can you help me understand what is being returned -- as it doesn't appear to be normal JSON. 您能帮助我了解所返回的内容吗-因为它似乎不是正常的JSON。 And what would be my best approach to parse this stream of data into a C# object. 而将这种数据流解析为C#对象的最佳方法是什么。

EDIT: I've confirmed that the data stream is valid JSON using http://www.freeformatter.com/json-validator.html and pasting in the sample lines above. 编辑:我已经使用http://www.freeformatter.com/json-validator.html并粘贴在上面的示例行中确认了数据流是有效的JSON。 So what I'm hoping for is a way to dynamically create an object based on the first line, then create a list of these objects with the values contained in the subsequent lines. 因此,我希望找到一种基于第一行动态创建对象,然后使用后续行中包含的值创建这些对象的列表的方法。

You are correct, this is not in typical JSON form. 您是正确的,这不是典型的JSON格式。 What you could do is create a collection of Dictionary<string, string> objects. 您可以做的是创建Dictionary<string, string>对象的集合。 Use the first part of the response to use as the keys of the dictionaries and then the values found in subsequent pieces of the result as the values of each dictionary. 使用响应的第一部分作为字典的键,然后在结果的后续片段中找到的值用作每个字典的值。

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

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