使用 JSON.stringify 和 encodeURIComponent 的组合


Using combination of JSON.stringify and encodeURIComponent

所以我有一个存储在变量中的JSON

var x = '{"time":"Friday, January 15, 2016 9:13 PM","keywords":"social","tweets_people":[{"text":"@FreeformTV I want to see what my social DNA looks like! #FreeformLaunch","username":"bbjeagle","user_image":"http://pbs.twimg.com/profile_images/681285613526515712/H6aB4gS__normal.jpg","timestamp":"2016-01-15T15:42:59.000Z","display_name":"JOY","language":"en","tweet_id":"688023580240670720","location":"united_states","location_details":{"country":"united_states","type":"country"},"retweet_count":0,"reach":531,"favorite_count":0,"sentiment":84,"entities":{}},{"text":"#IBM Scoops Up IRIS Analytics Fraud Detection Firm https://t.co/6oisTfWGNq #technology #enterprise #social #tc","username":"chriswrighttech","user_image":"http://pbs.twimg.com/profile_images/577224176542445568/1N2FMNHV_normal.jpg","timestamp":"2016-01-15T15:42:55.000Z","display_name":"Chris Wright","language":"en","tweet_id":"688023567221534721","location":null,"location_details":null,"retweet_count":0,"reach":458,"favorite_count":0,"sentiment":-50,"entities":{"urls":[{"display_url":"goo.gl/fb/weklj1","expanded_url":"http://goo.gl/fb/weklj1","url":"https://t.co/6oisTfWGNq"}]}},{"text":"Handbook of Statistical Modeling for the Social and Behavioral Sciences https://t.co/qCbResQLzK https://t.co/sLf2uhhUQd","username":"allwood_mary","user_image":"http://pbs.twimg.com/profile_images/653044576501035008/VWEwjp_N_normal.jpg","timestamp":"2016-01-15T15:42:49.000Z","display_name":"Mary Allwood","language":"en","tweet_id":"688023541023924227","location":null,"location_details":null,"retweet_count":0,"reach":52,"favorite_count":0,"sentiment":34,"entities":{"urls":[{"display_url":"uae-trip.info/p/tr/?query=ht…","expanded_url":"http://uae-trip.info/p/tr/?query=http://rover.ebay.com/rover/1/711-53200-19…=2&toolid=10039&campid=5337597384&item=151951539608&vectorid=229466&lgeo=1","url":"https://t.co/qCbResQLzK"}],"photo":[{"display_url":"pic.twitter.com/sLf2uhhUQd","expanded_url":"http://twitter.com/allwood_mary/status/688023541023924227/photo/1","type":"photo","media_url":"http://pbs.twimg.com/media/CYxZw96WsAAzzvk.jpg","resolution":{"w":242,"h":400,"resize":"fit"},"url":"https://t.co/sLf2uhhUQd"}]}},{"text":"Philasmicos R5 CRM (Standard) #Business Social Networking #Mac App... https://t.co/88YCSejyyE #socialnetworking","username":"marac00per","user_image":"http://pbs.twimg.com/profile_images/509110528208142336/Ah4ZC3vh_normal.jpeg","timestamp":"2016-01-15T15:42:42.000Z","display_name":"Mara Cooper","language":"en","tweet_id":"688023510111916032","location":"miami","location_details":{"country":"united_states","city":"miami","state":"florida","type":"city"},"retweet_count":0,"reach":33649,"favorite_count":0,"sentiment":0,"entities":{"urls":[{"display_url":"goo.gl/fb/7hwwhZ","expanded_url":"http://goo.gl/fb/7hwwhZ","url":"https://t.co/88YCSejyyE"}]}}],"tweets_media":[{"text":"The Peach app is dead, but it taught us this about social media: https://t.co/0IJsSnb2z2 https://t.co/iqu71DKk4L","username":"NewRepublic","user_image":"http://pbs.twimg.com/profile_images/631195731756388352/rXgYnC9S_normal.jpg","timestamp":"2016-01-15T15:40:22.000Z","display_name":"New Republic","language":"en","tweet_id":"688022921747533824","location":"washington","location_details":{"country":"united_states","state":"washington","type":"state"},"retweet_count":0,"reach":139506,"favorite_count":0,"sentiment":35,"entities":{"urls":[{"display_url":"bit.ly/234xz4y","expanded_url":"http://bit.ly/234xz4y","url":"https://t.co/0IJsSnb2z2"}],"photo":[{"display_url":"pic.twitter.com/iqu71DKk4L","expanded_url":"http://twitter.com/NewRepublic/status/688022921747533824/photo/1","type":"photo","media_url":"http://pbs.twimg.com/media/CYxZM7FWMAMxrSz.jpg","resolution":{"w":1024,"h":532,"resize":"fit"},"url":"https://t.co/iqu71DKk4L"}]}},{"text":"FG will ensure social welfare for the poor – Osinbajo https://t.co/KDwyJnbAgj via @todayng https://t.co/BPpQ7GE4Gi","username":"NigeriaNewsdesk","user_image":"http://pbs.twimg.com/profile_images/559635895909752832/5m7oAlGi_normal.png","timestamp":"2016-01-15T15:39:37.000Z","display_name":"Nigeria Newsdesk","language":"en","tweet_id":"688022736917118980","location":null,"location_details":null,"retweet_count":2,"reach":1070269,"favorite_count":0,"sentiment":0,"entities":{"urls":[{"display_url":"today.ng/news/national/…","expanded_url":"https://www.today.ng/news/national/66295/fg-will-ensure-social-welfare-for-the-poor-osinbajo?utm_source=dlvr.it_nnd&utm_medium=twitter","url":"https://t.co/KDwyJnbAgj"}],"photo":[{"display_url":"pic.twitter.com/BPpQ7GE4Gi","expanded_url":"http://twitter.com/NigeriaNewsdesk/status/688022736917118980/photo/1","type":"photo","media_url":"http://pbs.twimg.com/media/CYxZCKsWYAI0NZw.jpg","resolution":{"w":1024,"h":806,"resize":"fit"},"url":"https://t.co/BPpQ7GE4Gi"}]}},{"text":"Building your personal brand on social media is as much about listening as talking. https://t.co/U8VwyRYgD6 https://t.co/1qes1tfVMB","username":"FT","user_image":"http://pbs.twimg.com/profile_images/466972537704824832/eflEColL_normal.png","timestamp":"2016-01-15T15:09:50.000Z","display_name":"Financial Times","language":"en","tweet_id":"688015237958774784","location":null,"location_details":null,"retweet_count":0,"reach":2190381,"favorite_count":0,"sentiment":35,"entities":{"urls":[{"display_url":"on.ft.com/234vi9l","expanded_url":"http://on.ft.com/234vi9l","url":"https://t.co/U8VwyRYgD6"}],"photo":[{"display_url":"pic.twitter.com/1qes1tfVMB","expanded_url":"http://twitter.com/FT/status/688015237958774784/photo/1","type":"photo","media_url":"http://pbs.twimg.com/media/CYxSNpKWMAArwFh.jpg","resolution":{"w":600,"h":338,"resize":"fit"},"url":"https://t.co/1qes1tfVMB"}]}},{"text":"Who has decided that women cannot go in?: Ranjana Kumari, Director, Centre for Social Research on #SabrimalaRow @LRC_NDTV","username":"ndtv","user_image":"http://pbs.twimg.com/profile_images/570440108424171520/QuGYd7jH_normal.png","timestamp":"2016-01-15T14:46:17.000Z","display_name":"NDTV","language":"en","tweet_id":"688009314565550080","location":"india","location_details":{"country":"india","type":"country"},"retweet_count":15,"reach":5824509,"favorite_count":6,"sentiment":0,"entities":{}}],"topUsers":[["FreeformTV",678,"100%"],["AsMickymouse82",141,"-100%"],["ILoveMyDinoMatt",120,"0%"],["CloudataNow",79,"12%"],["TheSocialMs",79,"100%"],["mhsowrove",75,"100%"],["TSM_B2B",70,"100%"],["ExploreWeb2dot0",61,"-4%"],["Go2WebMarketing",61,"-4%"],["dreckbaerfrau",58,"100%"]],"locations":[["london",523],["new_york",321],["chicago",238],["los_angeles",204],["toronto",156],["kota",152],["atlanta",129],["miami",121],["mumbai",106],["boston",104]],"time_series_mentions":{"14/01/2016_15:43":1501,"14/01/2016_16:43":1592,"14/01/2016_17:43":1692,"14/01/2016_18:43":1477,"14/01/2016_19:43":1537,"14/01/2016_20:43":1864,"14/01/2016_21:43":1313,"14/01/2016_22:43":1448,"14/01/2016_23:43":1498,"15/01/2016_00:43":1289,"15/01/2016_01:43":1094,"15/01/2016_02:43":1313,"15/01/2016_03:43":1337,"15/01/2016_04:43":1075,"15/01/2016_05:43":1069,"15/01/2016_06:43":1053,"15/01/2016_07:43":808,"15/01/2016_08:43":844,"15/01/2016_09:43":524,"15/01/2016_10:43":973,"15/01/2016_11:43":1148,"15/01/2016_12:43":1313,"15/01/2016_13:43":1272,"15/01/2016_14:43":1277},"time_series_impressions":{"14/01/2016_15:43":9631791,"14/01/2016_16:43":24597649,"14/01/2016_17:43":42674232,"14/01/2016_18:43":61093552,"14/01/2016_19:43":70688408,"14/01/2016_20:43":47669000,"14/01/2016_21:43":24028898,"14/01/2016_22:43":76327602,"14/01/2016_23:43":50294599,"15/01/2016_00:43":74220157,"15/01/2016_01:43":45603520,"15/01/2016_02:43":29450814,"15/01/2016_03:43":46223938,"15/01/2016_04:43":15439446,"15/01/2016_05:43":19868983,"15/01/2016_06:43":82828364,"15/01/2016_07:43":98291003,"15/01/2016_08:43":11781541,"15/01/2016_09:43":8872385,"15/01/2016_10:43":4314408,"15/01/2016_11:43":14093326,"15/01/2016_12:43":10846241,"15/01/2016_13:43":8302175,"15/01/2016_14:43":18581107},"time_series_sentiment":{"14/01/2016_15:43":60,"14/01/2016_16:43":67,"14/01/2016_17:43":55,"14/01/2016_18:43":69,"14/01/2016_19:43":69,"14/01/2016_20:43":61,"14/01/2016_21:43":61,"14/01/2016_22:43":58,"14/01/2016_23:43":54,"15/01/2016_00:43":61,"15/01/2016_01:43":68,"15/01/2016_02:43":60,"15/01/2016_03:43":59,"15/01/2016_04:43":57,"15/01/2016_05:43":50,"15/01/2016_06:43":70,"15/01/2016_07:43":70,"15/01/2016_08:43":61,"15/01/2016_09:43":73,"15/01/2016_10:43":66,"15/01/2016_11:43":68,"15/01/2016_12:43":70,"15/01/2016_13:43":66,"15/01/2016_14:43":67},"relatedTopics":[["@Waze",1745,-76.9],["@Freeformtv",1229,100],["#Freeformlaunch",1215,100],["#Gopdebate",693,-14.1],["#Social",668,45.2],["@Arsenal",493,-99.2],["@Elnennym",490,-100],["#Socialmedia",364,66.4],["#Marketing",261,62.2],["@Realdonaldtrump",236,69],["@Gurmeetramrahim",223,100]],"media":{"mentions":145,"impressions":230059822,"sentiment":"20%"},"people":{"mentions":30311,"impressions":895723139,"sentiment":"25.2%"}}';

我知道这是一个有效的JSON(检查这里)

现在,当我尝试将其传递给encodeURIComponent()以使其URL安全时,它无法正确编码。我不知道为什么会这样。

我想将此 JSON 传递给一个 PHP 文件,我想使用 PHPExcel 从中生成一个 Excel 文件(使用此 JSON 数据生成)。我这样做是为了我可以将用户重定向到 php 页面,将此 JSON 作为 GET 参数传递,并将文件(因为 PHPExcel 的默认行为是它将文件存储在文件系统中)直接下载到客户端,而无需将其存储到文件系统。

顺便说一下,使用 AJAX 是否可以,对 PHP 文件进行 AJAX 调用,将此 JSON 作为 POST 数据传递,PHP 文件构建 EXCEL 并使用 excel 文件进行响应,而无需将其上传到服务器,以便可以直接下载到客户端。

你的字符串在编码之前很久就是 8951,请阅读这篇文章以了解为什么你的 GET 查询失败。将您的 Ajax 调用更改为 POST,这将起作用。

// x is not a string anymore, but a JSON object
var x= {"time":"Friday, January 15, 2016 9:13 PM", .... } ;
$.post ('myurl.php', x, function (ret, status) { ... }) ;