C#学习教程:C#JSON解析。 解析了逃脱的JSON分享


C#JSON解析。 解析了逃脱的JSON

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": 1n },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": 1n },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": 1n },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": 1n },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": 2n },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": 1n },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": 4n },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": 3n },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": 1n },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": 6n },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": 1n },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的一个例子(拉动喜欢的人和post中的评论。对我的Feed有最多评论的post是政治性的,所以对数据中的无知感到抱歉……)你可以看到,Unicode字符转换为\u0031 esque,引号看起来像\" ,新行看起来像\n等。

还有逃脱的转义字符,例如正文中的一条评论有\\"因为它是转义的转义字符(如果有意义的话)。

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

谢谢!

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

http://json.codeplex.com/

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

我使用.NET 4.5,这个方法没有unescape n和“。你可能会读取HttpWebResponse或类似的响应。你可能想将它的响应流转换为字节数组,然后将其转换为转换为一个字符串。

上述就是C#学习教程:C#JSON解析。 解析了逃脱的JSON分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—猴子技术宅(www.ssfiction.com)

本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ssfiction.com/ckf/1031412.html

发表评论

邮箱地址不会被公开。 必填项已用*标注