C# JSON 解析。解析转义的 JSON

发布于 2024-11-13 06:07:58 字数 11592 浏览 0 评论 0原文

Facebook 有一个新的批量请求图形 API,我正在尝试使用它。
JSON 看起来像这样:

[
   {
      "code": 200,
      "headers": [
         {
            "name": "Cache-Control",
            "value": "private, no-cache, no-store, must-revalidate"
         },
         {
            "name": "Connection",
            "value": "close"
         },
         {
            "name": "Content-Type",
            "value": "text/javascript; charset=UTF-8"
         },
         {
            "name": "ETag",
            "value": "\"abcd1234abcd1234abcd1234abcd1234\""
         },
         {
            "name": "Expires",
            "value": "Sat, 01 Jan 2000 00:00:00 GMT"
         },
         {
            "name": "P3P",
            "value": "CP=\"Facebook does not have a P3P policy. Learn why here: http://fb.me/p3p\""
         },
         {
            "name": "Pragma",
            "value": "no-cache"
         },
         {
            "name": "Set-Cookie",
            "value": "datr=abcd1234; expires=Sat, 01-Jun-2013 21:17:30 GMT; path=/; domain=.facebook.com; httponly"
         }
      ],
      "body": "{\n   \"data\": [\n      {\n         \"id\": \"526075017\",\n         \"name\": \"Jake Maldonado\"\n      }\n   ]\n}"
   },
   {
      "code": 200,
      "headers": [
         {
            "name": "Cache-Control",
            "value": "private, no-cache, no-store, must-revalidate"
         },
         {
            "name": "Connection",
            "value": "close"
         },
         {
            "name": "Content-Type",
            "value": "text/javascript; charset=UTF-8"
         },
         {
            "name": "ETag",
            "value": "\"abcd1234abcd1234abcd1234abcd1234\""
         },
         {
            "name": "Expires",
            "value": "Sat, 01 Jan 2000 00:00:00 GMT"
         },
         {
            "name": "P3P",
            "value": "CP=\"Facebook does not have a P3P policy. Learn why here: http://fb.me/p3p\""
         },
         {
            "name": "Pragma",
            "value": "no-cache"
         },
         {
            "name": "Set-Cookie",
            "value": "datr=abcd1234; expires=Sat, 01-Jun-2013 21:17:31 GMT; path=/; domain=.facebook.com; httponly"
         }
      ],
      "body": "{\n   \"data\": [\n      {\n         \"id\": \"107171896014674_102291113198394_15648\",\n         \"from\": {\n            \"name\": \"David Sicko Nichols\",\n            \"id\": \"100002260015742\"\n         },\n         \"message\": \"thas tha song fer my gunner...\",\n         \"created_time\": \"2011-06-02T21:06:29+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15649\",\n         \"from\": {\n            \"name\": \"Hannah Sellers\",\n            \"id\": \"100000688811712\"\n         },\n         \"message\": \"Does that mean they have found places to CUT that won't hurt the people even more?\",\n         \"created_time\": \"2011-06-02T21:06:30+0000\",\n         \"likes\": 1\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15650\",\n         \"from\": {\n            \"name\": \"Erin Dunn\",\n            \"id\": \"1571162232\"\n         },\n         \"message\": \"Not just reduced, but ELIMIATED, along with all functions performed by the federal government which are not constitutional.\",\n         \"created_time\": \"2011-06-02T21:06:52+0000\",\n         \"likes\": 1\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15651\",\n         \"from\": {\n            \"name\": \"Aaron Yox\",\n            \"id\": \"841285531\"\n         },\n         \"message\": \"i wonder if government knew that barowing money from the us people was ruled unconstitutional ?\",\n         \"created_time\": \"2011-06-02T21:06:57+0000\",\n         \"likes\": 1\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15652\",\n         \"from\": {\n            \"name\": \"David Osmalov\",\n            \"id\": \"1025396466\"\n         },\n         \"message\": \"Finally, a post that everyone can agree with!\",\n         \"created_time\": \"2011-06-02T21:07:03+0000\",\n         \"likes\": 1\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15653\",\n         \"from\": {\n            \"name\": \"Robert Whitelock\",\n            \"id\": \"1475652870\"\n         },\n         \"message\": \"Aaa... ya, Da!!\",\n         \"created_time\": \"2011-06-02T21:07:18+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15655\",\n         \"from\": {\n            \"name\": \"Michael Reece\",\n            \"id\": \"503494877\"\n         },\n         \"message\": \"yeah but.. well we all know they won't actually do that..\",\n         \"created_time\": \"2011-06-02T21:07:31+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15654\",\n         \"from\": {\n            \"name\": \"Craig Farmer\",\n            \"id\": \"510326379\"\n         },\n         \"message\": \"Is this not a very obvious suggestion? We all know the Government needs to reform, but they don't want to because one of their buddies has money tied up in the program. Its all about money and some 'person' keeping his/her job\",\n         \"created_time\": \"2011-06-02T21:07:31+0000\",\n         \"likes\": 2\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15657\",\n         \"from\": {\n            \"name\": \"Barby Parker\",\n            \"id\": \"100001832287011\"\n         },\n         \"message\": \"It's about frikken time...let's get some serious work done Government Accountability Office!!!!\",\n         \"created_time\": \"2011-06-02T21:08:08+0000\",\n         \"likes\": 1\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15662\",\n         \"from\": {\n            \"name\": \"John Craig\",\n            \"id\": \"100000471480073\"\n         },\n         \"message\": \"they will come up with an efficiency board. to help with efficiency. LOL!!!!!!\",\n         \"created_time\": \"2011-06-02T21:09:12+0000\",\n         \"likes\": 4\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15663\",\n         \"from\": {\n            \"name\": \"Michael Reece\",\n            \"id\": \"503494877\"\n         },\n         \"message\": \"let's just say that the post speaks for itself.. there had to be a government accountability office to point that out... and we'll just see how well that's taken.. can we all just agree that if they ACTUALLY cared about this, that it would have already been done? I mean, I cut people off the clock when labor gets high, so... what the hell have these people been doing? besides STEALING billions and trillions of dollars that you and I worked for? I don't know about you guys, but I hate it when people steal from my wallet and nobody does anything about it.\",\n         \"created_time\": \"2011-06-02T21:09:47+0000\",\n         \"likes\": 3\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15665\",\n         \"from\": {\n            \"name\": \"Jennifer Johnson\",\n            \"id\": \"1240748655\"\n         },\n         \"message\": \"Understatement... Queue 38 strategic VA initiatives related to claims processing.\",\n         \"created_time\": \"2011-06-02T21:10:14+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15666\",\n         \"from\": {\n            \"name\": \"Michael Huffman\",\n            \"id\": \"1346033926\"\n         },\n         \"message\": \"Increase inneficiency bro. You're going about this the wrong way.\",\n         \"created_time\": \"2011-06-02T21:10:21+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15668\",\n         \"from\": {\n            \"name\": \"Zachary Tanner\",\n            \"id\": \"1418226102\"\n         },\n         \"message\": \"Eliminate the DEA. All we need is the ATF\",\n         \"created_time\": \"2011-06-02T21:11:49+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15669\",\n         \"from\": {\n            \"name\": \"Sean Brontosaurus Merz\",\n            \"id\": \"1416480759\"\n         },\n         \"message\": \"\\\"Like\\\" if you want to decrease government inefficiency? THat's like saying \\\"Like\\\" if you think Hitler was a bad guy...The issue isn't whether or not you want to decrease government inefficiency. The issue is whether or not you can do that without hurting innocent people...Just saying.\",\n         \"created_time\": \"2011-06-02T21:13:16+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15670\",\n         \"from\": {\n            \"name\": \"Judy Zandt\",\n            \"id\": \"1071496566\"\n         },\n         \"message\": \"How about cutting the free rent for drug dealers? I had one living across the street for a while, and I just watched property values drop while they lived there. I do not appreciate being forced to support this type of person.\",\n         \"created_time\": \"2011-06-02T21:13:46+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15671\",\n         \"from\": {\n            \"name\": \"David Enders\",\n            \"id\": \"100000835155777\"\n         },\n         \"message\": \"Ok good start,next Homeland security.\",\n         \"created_time\": \"2011-06-02T21:13:48+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15672\",\n         \"from\": {\n            \"name\": \"Thomas Elliott\",\n            \"id\": \"1105406132\"\n         },\n         \"message\": \"Why do we need the BATF ??\",\n         \"created_time\": \"2011-06-02T21:13:49+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15674\",\n         \"from\": {\n            \"name\": \"Lori Shoenberger\",\n            \"id\": \"100002093863834\"\n         },\n         \"message\": \"End Dept of Energy and Education\",\n         \"created_time\": \"2011-06-02T21:15:14+0000\",\n         \"likes\": 1\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15675\",\n         \"from\": {\n            \"name\": \"Anthony Burton\",\n            \"id\": \"100000815275960\"\n         },\n         \"message\": \"LIKE this comment if you think inefficiency in the government should be ELIMINATED.\",\n         \"created_time\": \"2011-06-02T21:15:54+0000\",\n         \"likes\": 6\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15676\",\n         \"from\": {\n            \"name\": \"Tracy Barber\",\n            \"id\": \"1314090738\"\n         },\n         \"message\": \"Not to take people's jobs, but there is too much overlap.  Reduction in the TAX process would be a plus, although that is a major industry for some people.\",\n         \"created_time\": \"2011-06-02T21:17:30+0000\",\n         \"likes\": 1\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15683\",\n         \"from\": {\n            \"name\": \"Veronica Mason\",\n            \"id\": \"100000928641657\"\n         },\n         \"message\": \"Good luck with that.\",\n         \"created_time\": \"2011-06-02T21:25:48+0000\"\n      }\n   ]\n}"       }
]

如您所见,正文才是真正重要的,并且正文是完全转义的 JSON 字符串。在C#中,有什么好的方法来使用这个完全转义的JSON字符串吗?

这是来自 Facebook 的一个示例(从帖子中提取喜欢的人和评论。我的提要上评论最多的帖子是政治性的,很抱歉数据中的无知......),正如您所看到的, Unicode 字符转换为 \\u0031esque,引号看起来像 \\\",换行符看起来像 \\n,等等。

还有转义的转义字符,例如,正文中的注释之一具有 \\\\" 因为它是转义的转义字符(如果有意义的话)。

如果这些可以转换为未转义的甲酸盐以进行正常的 JSON 解析,那将是最好的。

谢谢你!

Facebook has a new Batch request Graph API that I'm trying to consume.
The JSON looks something like this:

[
   {
      "code": 200,
      "headers": [
         {
            "name": "Cache-Control",
            "value": "private, no-cache, no-store, must-revalidate"
         },
         {
            "name": "Connection",
            "value": "close"
         },
         {
            "name": "Content-Type",
            "value": "text/javascript; charset=UTF-8"
         },
         {
            "name": "ETag",
            "value": "\"abcd1234abcd1234abcd1234abcd1234\""
         },
         {
            "name": "Expires",
            "value": "Sat, 01 Jan 2000 00:00:00 GMT"
         },
         {
            "name": "P3P",
            "value": "CP=\"Facebook does not have a P3P policy. Learn why here: http://fb.me/p3p\""
         },
         {
            "name": "Pragma",
            "value": "no-cache"
         },
         {
            "name": "Set-Cookie",
            "value": "datr=abcd1234; expires=Sat, 01-Jun-2013 21:17:30 GMT; path=/; domain=.facebook.com; httponly"
         }
      ],
      "body": "{\n   \"data\": [\n      {\n         \"id\": \"526075017\",\n         \"name\": \"Jake Maldonado\"\n      }\n   ]\n}"
   },
   {
      "code": 200,
      "headers": [
         {
            "name": "Cache-Control",
            "value": "private, no-cache, no-store, must-revalidate"
         },
         {
            "name": "Connection",
            "value": "close"
         },
         {
            "name": "Content-Type",
            "value": "text/javascript; charset=UTF-8"
         },
         {
            "name": "ETag",
            "value": "\"abcd1234abcd1234abcd1234abcd1234\""
         },
         {
            "name": "Expires",
            "value": "Sat, 01 Jan 2000 00:00:00 GMT"
         },
         {
            "name": "P3P",
            "value": "CP=\"Facebook does not have a P3P policy. Learn why here: http://fb.me/p3p\""
         },
         {
            "name": "Pragma",
            "value": "no-cache"
         },
         {
            "name": "Set-Cookie",
            "value": "datr=abcd1234; expires=Sat, 01-Jun-2013 21:17:31 GMT; path=/; domain=.facebook.com; httponly"
         }
      ],
      "body": "{\n   \"data\": [\n      {\n         \"id\": \"107171896014674_102291113198394_15648\",\n         \"from\": {\n            \"name\": \"David Sicko Nichols\",\n            \"id\": \"100002260015742\"\n         },\n         \"message\": \"thas tha song fer my gunner...\",\n         \"created_time\": \"2011-06-02T21:06:29+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15649\",\n         \"from\": {\n            \"name\": \"Hannah Sellers\",\n            \"id\": \"100000688811712\"\n         },\n         \"message\": \"Does that mean they have found places to CUT that won't hurt the people even more?\",\n         \"created_time\": \"2011-06-02T21:06:30+0000\",\n         \"likes\": 1\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15650\",\n         \"from\": {\n            \"name\": \"Erin Dunn\",\n            \"id\": \"1571162232\"\n         },\n         \"message\": \"Not just reduced, but ELIMIATED, along with all functions performed by the federal government which are not constitutional.\",\n         \"created_time\": \"2011-06-02T21:06:52+0000\",\n         \"likes\": 1\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15651\",\n         \"from\": {\n            \"name\": \"Aaron Yox\",\n            \"id\": \"841285531\"\n         },\n         \"message\": \"i wonder if government knew that barowing money from the us people was ruled unconstitutional ?\",\n         \"created_time\": \"2011-06-02T21:06:57+0000\",\n         \"likes\": 1\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15652\",\n         \"from\": {\n            \"name\": \"David Osmalov\",\n            \"id\": \"1025396466\"\n         },\n         \"message\": \"Finally, a post that everyone can agree with!\",\n         \"created_time\": \"2011-06-02T21:07:03+0000\",\n         \"likes\": 1\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15653\",\n         \"from\": {\n            \"name\": \"Robert Whitelock\",\n            \"id\": \"1475652870\"\n         },\n         \"message\": \"Aaa... ya, Da!!\",\n         \"created_time\": \"2011-06-02T21:07:18+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15655\",\n         \"from\": {\n            \"name\": \"Michael Reece\",\n            \"id\": \"503494877\"\n         },\n         \"message\": \"yeah but.. well we all know they won't actually do that..\",\n         \"created_time\": \"2011-06-02T21:07:31+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15654\",\n         \"from\": {\n            \"name\": \"Craig Farmer\",\n            \"id\": \"510326379\"\n         },\n         \"message\": \"Is this not a very obvious suggestion? We all know the Government needs to reform, but they don't want to because one of their buddies has money tied up in the program. Its all about money and some 'person' keeping his/her job\",\n         \"created_time\": \"2011-06-02T21:07:31+0000\",\n         \"likes\": 2\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15657\",\n         \"from\": {\n            \"name\": \"Barby Parker\",\n            \"id\": \"100001832287011\"\n         },\n         \"message\": \"It's about frikken time...let's get some serious work done Government Accountability Office!!!!\",\n         \"created_time\": \"2011-06-02T21:08:08+0000\",\n         \"likes\": 1\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15662\",\n         \"from\": {\n            \"name\": \"John Craig\",\n            \"id\": \"100000471480073\"\n         },\n         \"message\": \"they will come up with an efficiency board. to help with efficiency. LOL!!!!!!\",\n         \"created_time\": \"2011-06-02T21:09:12+0000\",\n         \"likes\": 4\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15663\",\n         \"from\": {\n            \"name\": \"Michael Reece\",\n            \"id\": \"503494877\"\n         },\n         \"message\": \"let's just say that the post speaks for itself.. there had to be a government accountability office to point that out... and we'll just see how well that's taken.. can we all just agree that if they ACTUALLY cared about this, that it would have already been done? I mean, I cut people off the clock when labor gets high, so... what the hell have these people been doing? besides STEALING billions and trillions of dollars that you and I worked for? I don't know about you guys, but I hate it when people steal from my wallet and nobody does anything about it.\",\n         \"created_time\": \"2011-06-02T21:09:47+0000\",\n         \"likes\": 3\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15665\",\n         \"from\": {\n            \"name\": \"Jennifer Johnson\",\n            \"id\": \"1240748655\"\n         },\n         \"message\": \"Understatement... Queue 38 strategic VA initiatives related to claims processing.\",\n         \"created_time\": \"2011-06-02T21:10:14+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15666\",\n         \"from\": {\n            \"name\": \"Michael Huffman\",\n            \"id\": \"1346033926\"\n         },\n         \"message\": \"Increase inneficiency bro. You're going about this the wrong way.\",\n         \"created_time\": \"2011-06-02T21:10:21+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15668\",\n         \"from\": {\n            \"name\": \"Zachary Tanner\",\n            \"id\": \"1418226102\"\n         },\n         \"message\": \"Eliminate the DEA. All we need is the ATF\",\n         \"created_time\": \"2011-06-02T21:11:49+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15669\",\n         \"from\": {\n            \"name\": \"Sean Brontosaurus Merz\",\n            \"id\": \"1416480759\"\n         },\n         \"message\": \"\\\"Like\\\" if you want to decrease government inefficiency? THat's like saying \\\"Like\\\" if you think Hitler was a bad guy...The issue isn't whether or not you want to decrease government inefficiency. The issue is whether or not you can do that without hurting innocent people...Just saying.\",\n         \"created_time\": \"2011-06-02T21:13:16+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15670\",\n         \"from\": {\n            \"name\": \"Judy Zandt\",\n            \"id\": \"1071496566\"\n         },\n         \"message\": \"How about cutting the free rent for drug dealers? I had one living across the street for a while, and I just watched property values drop while they lived there. I do not appreciate being forced to support this type of person.\",\n         \"created_time\": \"2011-06-02T21:13:46+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15671\",\n         \"from\": {\n            \"name\": \"David Enders\",\n            \"id\": \"100000835155777\"\n         },\n         \"message\": \"Ok good start,next Homeland security.\",\n         \"created_time\": \"2011-06-02T21:13:48+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15672\",\n         \"from\": {\n            \"name\": \"Thomas Elliott\",\n            \"id\": \"1105406132\"\n         },\n         \"message\": \"Why do we need the BATF ??\",\n         \"created_time\": \"2011-06-02T21:13:49+0000\"\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15674\",\n         \"from\": {\n            \"name\": \"Lori Shoenberger\",\n            \"id\": \"100002093863834\"\n         },\n         \"message\": \"End Dept of Energy and Education\",\n         \"created_time\": \"2011-06-02T21:15:14+0000\",\n         \"likes\": 1\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15675\",\n         \"from\": {\n            \"name\": \"Anthony Burton\",\n            \"id\": \"100000815275960\"\n         },\n         \"message\": \"LIKE this comment if you think inefficiency in the government should be ELIMINATED.\",\n         \"created_time\": \"2011-06-02T21:15:54+0000\",\n         \"likes\": 6\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15676\",\n         \"from\": {\n            \"name\": \"Tracy Barber\",\n            \"id\": \"1314090738\"\n         },\n         \"message\": \"Not to take people's jobs, but there is too much overlap.  Reduction in the TAX process would be a plus, although that is a major industry for some people.\",\n         \"created_time\": \"2011-06-02T21:17:30+0000\",\n         \"likes\": 1\n      },\n      {\n         \"id\": \"107171896014674_102291113198394_15683\",\n         \"from\": {\n            \"name\": \"Veronica Mason\",\n            \"id\": \"100000928641657\"\n         },\n         \"message\": \"Good luck with that.\",\n         \"created_time\": \"2011-06-02T21:25:48+0000\"\n      }\n   ]\n}"       }
]

As you can see, the body is all that is really important, and the body is a fully escaped JSON string. In C#, is there any good ways to consume this fully escaped JSON string?

This is an example from Facebook (pulling the people who like and the comments from a post. The post that had the most comments on my feed was something political, so sorry for the ignorance in the data...) and as you can see, Unicode chars are converted to \\u0031esque, quote marks look like \\\", new-lines look like \\n, etc.

There are also escaped escaped chars, for example one of the comments in the body has \\\\" because it is the escaped escaped char (if that makes sense).

If those can be converted to their unescaped formate for normal JSON parsing, that would be the best.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

祁梦 2024-11-20 06:07:58

使用 Newtonsoft.Json。该库非常适合解析和生成 JSON。

http://json.codeplex.com/

Use Newtonsoft.Json. The library is really good for parsing and generating JSON.

http://json.codeplex.com/

荆棘i 2024-11-20 06:07:58

有评论建议使用 Regex.Unescape()。

我使用 .NET 4.5,此方法不会转义 \n 和 \"。
您可能会阅读来自 HttpWebResponse 或类似内容的响应。您可能希望将其响应流转换为字节数组,然后将其转换为字符串。

There is a comment recommedning to use a Regex.Unescape().

I work with the .NET 4.5 and this method does not unescape \n and \".
You probably read a response from HttpWebResponse or alike. You may want to convert it's response stream to byte array and then play a little bit with converting it to a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文