简体   繁体   English

使用php从网络服务器读取时,JSON文件返回null。

[英]JSON file returning null when read from webserver using php.

I have a .json file uploaded on my web server with valid syntax.( http://www.isoivit.in/Details.json ). 我使用有效的语法在我的Web服务器上上传了一个.json文件。( http://www.isoivit.in/Details.json )。

I want to edit it using php. 我想使用php编辑它。 My php code is as given below: 我的PHP代码如下:

 <?php $jsonString = file_get_contents('http:/www.isoivit.in/json/Details.json'); preg_replace("/\\s+/", " ", $jsonString); /*$jsonString='{ "events_info":{ "total_events": "3", "events":[ { "id": "1", "name": "JUST FAST", "image":"events_image1", "short_des":"JUST FAST(Finding an Answer is So Tricky) It is an interesting and enthusiastic game. You must be so fast in finding the tricky answers.", "med_des":" It is an interesting and enthusiastic game.\\r\\nYou must be so fast in finding the tricky answers. It has four rounds. In the first 2 rounds the questionnaire would be funny and enjoyable which includes few audio and video clips also.\\r\\nThere will be a rapid fire round based on themes of Riviera and also interesting tasks to be done in the final round. Exciting rewards will be given after the clearance of 2nd and 3rd rounds.So gear up people, to experience the boundless fun. \\r\\n", "full_des":"1) It has 4 rounds.\\r\\n2) In first round 5 questions will be asked and there will be a time limit. The questions will not be general knowledge or current affairs based. They will be funny riddles and tricky questions.\\r \\n3)In second round questions will be mostly based on audios and visuals. Eg: an audio of any cartoon character will be played and they have to recognize that cartoon character.\\r\\n4)They can use lifelines in first 2 rounds like skip a question, take audience help.\\r\\n5) The third round will be a rapid fire round. In this round we will provide them few topics and they need to choose one of them(those topics will be related to the theme of Riviera, like Indian cricket ,etc.).The participant will be asked questions on the topic of his choice.\\r\\n6)The fourth round will be held on the fourth day. The participant will be given 3 tasks to perform. They are:\\r\\n---> They will be given riddles in different languages other than English and Hindi. They need to know which language it is and solve the riddle. \\r\\n--->They need to bring outdated posters from the whole VIT. Specific time limit will be given. \\r\\n---> They need to get autographs or photos of tags of organizers and managers of Riviera.\\r\\n7)Apart from this audience also will have their set of interesting questions. Their answers will be taken into consideration and rewards are given to the lucky persons.\\r\\n", "details":" Prize Money :\\r\\n\\r\\n1st- Rs.2000\\/- \\r\\n2nd- Rs.1500\\/- \\r\\n3rd- Rs.1000\\/-\\r\\nRegistration fee:\\r\\n\\r\\nRs.100 per member\\r\\nEvent Coordinators\\r\\n\\r\\nN.Sai Mounika \\r\\nmouni123.nekkalapudi@gmail.com \\r\\n " }, { "id": "2", "name": "Speeling Bee", "image":"events_image2", "short_des":"Spelling Bee is a competition in which contestants are asked to spell a broad selection of words, usually with a varying degree of difficulty.", "med_des":" Spelling Bee is a competition in which contestants are asked to spell a broad selection of words, usually with a varying degree of difficulty.\\r\\nIt has three rounds.\\r\\nFirst round- \\r\\nIn first round, audio of 25 words is played and participants need to write them on paper.Based on the score top ten people are screened. \\r\\n\\r\\nSecond round- \\r\\nIn second round, 5 questions are asked to each 10 participants. \\r\\n\\r\\nThird round- \\r\\nThird or final round is a buzzer round.10 questions are asked. \\r\\nThere are negative marks for wrong answers in this round. \\r \\nBased on scores of 2nd and third round,winner and runner-up are decided.\\r\\n", "full_des":"", "details":" \\r\\nPrize Money :\\r\\n\\r\\n1st- Rs.2000\\/- \\r\\n2nd- Rs.1500\\/- \\r\\n3rd- Rs.1000\\/-\\r\\nRegistration fee:\\r\\n\\r\\nRs.100 per member\\r\\nEvent Coordinators\\r\\n\\r\\nPranay Yenigandla \\r\\npranay.pranu111@gmail.com\\r\\nShreyash Saurabh \\r\\n " }, { "id": "3", "name": "Be Crazy", "image":"events_image3", "short_des":" Do you wanna do something crazy? !!! Here is our event \\"Be Crazy\\"-Crazyness in peaks.", "med_des":" Do you wanna do something crazy? !!! Here is our event \\"Be Crazy\\"-Crazyness in peaks.\\r\\nDo you think that you are a bit crazy among your friends..!!!Here is our event for this RIVEIRA2015.\\r\\nIn this event you are given some crazy tasks to perform with time limit in levels of 1,2 &3.like \\"Shot A Celebrete -in this event u have to get a selfie with organisers and famous personalities of VIT \\"\\r\\n", "full_des":" We have 3 rounds in this event.\\r\\n1st round has 10 simpler tasks.\\r\\nExamples:\\r\\n>>Forming towers with paper cups\\r\\n>>Lemon spoon race\\r \\n>>Walk in grills without touching\\r\\n>>Run the race with legs tied together\\r\\n>>Lift the rod holding from edge\\r\\n>>Skipping race\\r\\n2nd round has 5 tasks.\\r\\nExamples:\\r \\n>>Sac race\\r\\n>>Hold the wet brick with two fingers\\r\\n>>Get a photo with organizers of RIVEIRA (10)\\r\\n3rd round has 2-3 tasks like\\r\\nExamples:\\r\\n>>catch the paper between fingers when it is released\\r\\nEach task is given its own credits. The time taken to complete every task is noted for every team. Teams which are leading in that task in terms of time will get credits. At the end of first round depending on the credits they will be qualified to the second round. In the similar manner people will be selected to the third round also. winners are announced after the third round depending on their credits.\\r\\nWe will select top 20 teams from round 1 that will qualify to round 2.\\r\\nWe will select top10 teams from round 2 that will qualify round 3.\\r\\nWe will decide qualifiers for each round on basis of points and time taken to complete a task. \\r\\n", "details":" Prize Money :\\r\\n\\r\\n1st- Rs.3000\\/-\\r\\n2nd- Rs.2000\\/-\\r\\n3rd- Rs.1000\\/-\\r\\nRegistration fee:\\r\\n\\r\\nRs 150\\/- per team\\r\\nNo of participants per team:\\r\\n\\r\\n5(Maximum)\\r\\nEvent Coordinators\\r\\n \\r\\nK. Jaswanth Kumar\\r\\njaswanthjaan@gmail.com\\r\\nAnkush Saini\\r\\nankushsaini88@gmail.com\\r\\n" } ] }, "archives_info":{ "total_events": "2", "events":[ { "id": "1", "name": "Explore Matlab", "image":"archives_image1", "short_des":" It is a five day free workshop.\\r\\nMATLAB (matrix laboratory) is a multi-paradigm numerical computing environment and fourth-generation programming language. Developed by MathWorks, MATLAB allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages, including C, C++, Java, Fortran and Python.\\r\\nThe workshop aims to develop the vision and skills MATLAB.The workshop is one of the huge success with approximately 400 students attending the workshop every year .\\r\\n", "med_des":"", "full_des":"", "details":"1) Basics of MATLAB.\\r\\n2) MATLAB Simulink.\\r\\n3) Fundamentals of Signals & Systems using MATLAB.\\r\\n4) Probability & Random Processes using MATLAB.\\r\\n5) Analysis & Design of Control System using MATLAB.\\r\\n6) Modelling & Simulation of Signal Processing Application.\\r\\n7) Artificial Neural Network using MATLAB.\\r\\n" }, { "id": "2", "name": "See The C", "image":"archives_image2", "short_des":" It is a free workshop for all the students to develop their programming skills used in the field of Computer Science. C is the most widely used programming languages of all time and C compilers are available for the majority of available computer architecture and operating systems.\\r\\nThis workshop is to help first years to learn a new language in which most of them find difficulty to learn.\\r\\n", "med_des":"", "full_des":"", "details":"1) C Language is a general-purpose programming language .C has facilities for structured programming and allows lexical variable scope and recursion.\\r\\n2) Its design provides constructs that map efficiently to typical machine instructions and therefore it has found lasting use in applications that had formerly been coded in assembly language.\\r\\n3) The C programming language uses libraries as its primary method of extension. In C, a library is a set of functions contained within a single \\"archive\\" file. Each library typically has a header file, which contains the prototypes of the functions contained within the library that may be used by a program, and declarations of special data types and macro symbols used with these functions.\\r\\n4) C has been used successfully for every type of programming problem imaginable from operating systems to spreadsheets to expert systems - and efficient compilers are available for machines ranging in power from the Apple Macintosh to the Cray supercomputers.\\r\\n5) Tools have been created to help C programmers avoid some of the problems inherent in the language, such as statements with undefined behavior or statements that are not a good practice because they are likely to result in unintended behavior or run-time errors.\\r\\n6) Despite its low-level capabilities, the language was designed to encourage cross-platform programming. A standards-compliant and portably written C program can be compiled for a very wide variety of computer platforms and operating systems with few changes to its source code\\r\\n7) With regards to personal computers Microsoft C for IBM (or clones) PC\\'s. and Borlands C are seen to be the two most commonly used systems. However, the latest version of Microsoft C is now considered to be the most powerful and efficient C compiler for personal computers.\\r\\n" } ] }, "riviera_info":{ "total_events": "3", "events":[ { "id": "1", "name": "JUST FAST", "image":"riviera_image1", "short_des":"JUST FAST(Finding an Answer is So Tricky) It is an interesting and enthusiastic game. You must be so fast in finding the tricky answers.", "med_des":" It is an interesting and enthusiastic game.\\r\\nYou must be so fast in finding the tricky answers. It has four rounds. In the first 2 rounds the questionnaire would be funny and enjoyable which includes few audio and video clips also.\\r\\nThere will be a rapid fire round based on themes of Riviera and also interesting tasks to be done in the final round. Exciting rewards will be given after the clearance of 2nd and 3rd rounds.So gear up people, to experience the boundless fun. \\r\\n", "full_des":"1) It has 4 rounds.\\r\\n2) In first round 5 questions will be asked and there will be a time limit. The questions will not be general knowledge or current affairs based. They will be funny riddles and tricky questions.\\r \\n3)In second round questions will be mostly based on audios and visuals. Eg: an audio of any cartoon character will be played and they have to recognize that cartoon character.\\r\\n4)They can use lifelines in first 2 rounds like skip a question, take audience help.\\r\\n5) The third round will be a rapid fire round. In this round we will provide them few topics and they need to choose one of them(those topics will be related to the theme of Riviera, like Indian cricket ,etc.).The participant will be asked questions on the topic of his choice.\\r\\n6)The fourth round will be held on the fourth day. The participant will be given 3 tasks to perform. They are:\\r\\n---> They will be given riddles in different languages other than English and Hindi. They need to know which language it is and solve the riddle. \\r\\n--->They need to bring outdated posters from the whole VIT. Specific time limit will be given. \\r\\n---> They need to get autographs or photos of tags of organizers and managers of Riviera.\\r\\n7)Apart from this audience also will have their set of interesting questions. Their answers will be taken into consideration and rewards are given to the lucky persons.\\r\\n", "details":" Prize Money :\\r\\n\\r\\n1st- Rs.2000\\/- \\r\\n2nd- Rs.1500\\/- \\r\\n3rd- Rs.1000\\/-\\r\\nRegistration fee:\\r\\n\\r\\nRs.100 per member\\r\\nEvent Coordinators\\r\\n\\r\\nN.Sai Mounika \\r\\nmouni123.nekkalapudi@gmail.com \\r\\n " }, { "id": "2", "name": "Speeling Bee", "image":"riviera_image2", "short_des":"Spelling Bee is a competition in which contestants are asked to spell a broad selection of words, usually with a varying degree of difficulty.", "med_des":" Spelling Bee is a competition in which contestants are asked to spell a broad selection of words, usually with a varying degree of difficulty.\\r\\nIt has three rounds.\\r\\nFirst round- \\r\\nIn first round, audio of 25 words is played and participants need to write them on paper.Based on the score top ten people are screened. \\r\\n\\r\\nSecond round- \\r\\nIn second round, 5 questions are asked to each 10 participants. \\r\\n\\r\\nThird round- \\r\\nThird or final round is a buzzer round.10 questions are asked. \\r\\nThere are negative marks for wrong answers in this round. \\r \\nBased on scores of 2nd and third round,winner and runner-up are decided.\\r\\n", "full_des":"", "details":" \\r\\nPrize Money :\\r\\n\\r\\n1st- Rs.2000\\/- \\r\\n2nd- Rs.1500\\/- \\r\\n3rd- Rs.1000\\/-\\r\\nRegistration fee:\\r\\n\\r\\nRs.100 per member\\r\\nEvent Coordinators\\r\\n\\r\\nPranay Yenigandla \\r\\npranay.pranu111@gmail.com\\r\\nShreyash Saurabh \\r\\n " }, { "id": "3", "name": "Be Crazy", "image":"riviera_image3", "short_des":" Do you wanna do something crazy? !!! Here is our event \\"Be Crazy\\"-Crazyness in peaks.", "med_des":" Do you wanna do something crazy? !!! Here is our event \\"Be Crazy\\"-Crazyness in peaks.\\r\\nDo you think that you are a bit crazy among your friends..!!!Here is our event for this RIVEIRA2015.\\r\\nIn this event you are given some crazy tasks to perform with time limit in levels of 1,2 &3.like \\"Shot A Celebrete -in this event u have to get a selfie with organisers and famous personalities of VIT \\"\\r\\n", "full_des":" We have 3 rounds in this event.\\r\\n1st round has 10 simpler tasks.\\r\\nExamples:\\r\\n>>Forming towers with paper cups\\r\\n>>Lemon spoon race\\r \\n>>Walk in grills without touching\\r\\n>>Run the race with legs tied together\\r\\n>>Lift the rod holding from edge\\r\\n>>Skipping race\\r\\n2nd round has 5 tasks.\\r\\nExamples:\\r \\n>>Sac race\\r\\n>>Hold the wet brick with two fingers\\r\\n>>Get a photo with organizers of RIVEIRA (10)\\r\\n3rd round has 2-3 tasks like\\r\\nExamples:\\r\\n>>catch the paper between fingers when it is released\\r\\nEach task is given its own credits. The time taken to complete every task is noted for every team. Teams which are leading in that task in terms of time will get credits. At the end of first round depending on the credits they will be qualified to the second round. In the similar manner people will be selected to the third round also. winners are announced after the third round depending on their credits.\\r\\nWe will select top 20 teams from round 1 that will qualify to round 2.\\r\\nWe will select top10 teams from round 2 that will qualify round 3.\\r\\nWe will decide qualifiers for each round on basis of points and time taken to complete a task. \\r\\n", "details":" Prize Money :\\r\\n\\r\\n1st- Rs.3000\\/-\\r\\n2nd- Rs.2000\\/-\\r\\n3rd- Rs.1000\\/-\\r\\nRegistration fee:\\r\\n\\r\\nRs 150\\/- per team\\r\\nNo of participants per team:\\r\\n\\r\\n5(Maximum)\\r\\nEvent Coordinators\\r\\n \\r\\nK. Jaswanth Kumar\\r\\njaswanthjaan@gmail.com\\r\\nAnkush Saini\\r\\nankushsaini88@gmail.com\\r\\n" } ] } } ';*/ echo $jsonString; echo '...............................................'; $data=json_decode($jsonString,true); var_dump($data); //$data1=json_decode($data['events_info'],true); //var_dump($data1); $var1= $data['events_info']; $var2= $var1['events']; $var3=$var2[0]; echo $var3['name']; echo json_last_error();?> 

In the comment section of my php script is the whole json string copied from web. 在我的PHP脚本的注释部分中是从Web复制的整个json字符串。 The problem I am facing is that when I use json_decode on the string returned by file_get_contents('http:/www.isoivit.in/json/Details.json') it returns NULL, while when I use json_decode on the string which is copied from same json file I get a valid output what I desire. 我面临的问题是,当我在由file_get_contents('http:/www.isoivit.in/json/Details.json')返回的字符串上使用json_decode时,它返回NULL,而当我在复制的字符串上使用json_decode时从同一个json文件中,我得到了想要的有效输出。 This means that my php code is able to read json file from web but unable to decode it. 这意味着我的php代码能够从网络读取json文件,但无法对其进行解码。 I get a json last error 4(SYNTAX ERROR). 我收到json最后的错误4(语法错误)。 Though I have checked for all errors and its running feasibly on my android application. 虽然我已经检查了所有错误,并且在我的android应用程序上可行地运行了它。 Please help. 请帮忙。

You have only one / in the http:// , you should change to http://www.isoivit.in/json/Details.json and you get NULL because of the \\' from PC\\'s (it decodes with simple unescaped ' then it get's into conflict with json_decode . You could do: 你只有一个/http:// ,你应该改变对http://www.isoivit.in/json/Details.json你因为得到NULL \\'PC\\'s (它与简单的解码未转义的'则与json_decode发生冲突。您可以执行以下操作:

$jsonString =  file_get_contents('http://www.isoivit.in/json/Details.json');
$jsonString = str_replace("\\'", "\\\\'", $jsonString); // double encode the single quote
$jsonString = json_decode($jsonString, true);

print_r($jsonString);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM