需要帮助使用“root”无JSON对象与Json.Net和JArrays

时间:2022-08-26 11:54:44

I'm using the ExportAPI from MailChimp. It sends back a "root"-less Json string like so:

我正在使用MailChimp的ExportAPI。它发回一个“root”无Json字符串,如下所示:

["Email Address", "First Name", "Last Name"]
["jeff@mydomain.com", "Jeff", "Johnson"]
["tubbs@mydomain.com", "Tubbs", "McGraw"]

No brackets, nothing- just a couple of arrays. Loading it into a JArray only picks up the first object:

没有括号,没有 - 只有几个数组。将其加载到JArray中只会获取第一个对象:

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?)

我希望将字符串的内容复制到数据库中,并且需要使用LINQ访问响应。有关使用Json对象的正确方法的任何建议,如上所示(使用Json.net或其他?)

2 个解决方案

#1


1  

Pad the string with a root element, just add '[' and ']'?

用根元素填充字符串,只需添加'['和']'?

#2


0  

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.

我不熟悉在C#/ Linq中这样做,但文档页面上的PHP示例正是如此。

#1


1  

Pad the string with a root element, just add '[' and ']'?

用根元素填充字符串,只需添加'['和']'?

#2


0  

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.

我不熟悉在C#/ Linq中这样做,但文档页面上的PHP示例正是如此。