2011-06-02 284 views
1

Facebook有一個新的批量請求圖形API,我試圖消費。
的JSON看起來是這樣的:C#JSON解析。解析轉義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的一個例子(拉動喜歡的人和來自帖子的評論,對我的Feed有最多評論的帖子是政治性的,所以對數據的無知感到抱歉...)和你可以看到,Unicode的字符轉換爲\\u0031式的,引號的樣子\\\",新線看起來像\\n

還有一些逃脫的轉義字符,例如在體內的意見之一具有\\\\"因爲它是逃逸的char(如果這是有道理的話)。

如果這些可以轉換爲其正常的JSON解析他們的非轉義的甲酸,這將是最好的。

謝謝!

回答

1

使用Newtonsoft.Json。該庫非常適合解析和生成JSON。

http://json.codeplex.com/

+0

這就是我目前使用的庫,如何使用它將字符串轉換爲我想要的那樣? – 2011-06-02 21:45:24

+1

您首先解析文檔並獲取正文字符串。然後把它放到解析器中 – CodesInChaos 2011-06-02 21:53:48

+0

我明白了,你是對的,但是我在找的是'Regex.Unescape()'方法,我得到了一個解析異常,因爲所有的特殊字符仍然是逃過一劫。 – 2011-06-02 22:21:09

0

有評論recommedning使用Regex.Unescape()。

我使用.NET 4.5,此方法不會使用unescape \ n和\「 您可能會閱讀HttpWebResponse或類似的響應,您可能希望將其響應流轉換爲字節數組,然後播放一下將其轉換爲字符串