需要帮助将“root”-less JSON 对象与 Json.Net 和 JArrays 一起使用
我正在使用 MailChimp 的 ExportAPI。它发送回一个无“根”的 Json 字符串,如下所示:
["Email Address", "First Name", "Last Name"]
["[email protected]", "Jeff", "Johnson"]
["[email protected]", "Tubbs", "McGraw"]
没有括号,什么都没有 - 只是几个数组。将其加载到 JArray 中只会获取第一个对象:
JArray jsonArray = new JArray.Parse(jsonResponse);
Console.WriteLine(jsonArray);
//Outputs:
//Email Address
//First Name
//Last Name
我希望将字符串的内容复制到数据库中,并且需要使用 LINQ 访问响应。有关使用 Json 对象的正确方法(如我上面所示)的任何建议(使用 Json.net 或其他方式?)
I'm using the ExportAPI from MailChimp. It sends back a "root"-less Json string like so:
["Email Address", "First Name", "Last Name"]
["[email protected]", "Jeff", "Johnson"]
["[email protected]", "Tubbs", "McGraw"]
No brackets, nothing- just a couple of arrays. Loading it into a JArray only picks up the first object:
JArray jsonArray = new JArray.Parse(jsonResponse);
Console.WriteLine(jsonArray);
//Outputs:
//Email Address
//First Name
//Last Name
I'm hoping to copy the contents of the string into a database and need to access the response with LINQ. Any suggestions as to the correct way to work with a Json Object like I've shown above (using Json.net or otherwise?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用根元素填充字符串,只需添加“[”和“]”?
Pad the string with a root element, just add '[' and ']'?
正如文档中提到的,这种行为实际上完全是故意的。原因是列表数据的完整转储很容易太大而无法始终将其放入内存并解析它。因此,在给定返回格式的情况下,您应该使用换行符作为分隔符(或以这种方式从线路中读取它),单独解析每个对象,然后对它们执行任何您需要的操作。
我不熟悉在 C#/Linq 中执行此操作,但文档页面上的 PHP 示例正是这样做的。
This behavior is actually completely on purpose as mentioned in the docs. The reasoning is that a full dump of a list's data can easily be way too large to consistently fit it in memory and parse it. As such, and given the return format, you're expected to use a newline as the delimiter (or read it off the wire that way), parse each object individually, and then do whatever you need with them.
I am not familiar with doing that in C#/Linq, but the PHP example on the docs page does exactly that.