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

JSON file returning null when read from webserver using php.

我在我的 Web 服务器上上传了一个语法有效的 .json 文件。( http://www.isoivit.in/Details.json)。

我想使用 php 编辑它。我的 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 F.A.S.T",
                 "image":"events_image1",
   "short_des":"JUST F.A.S.T(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 F.A.S.T",
                 "image":"riviera_image1",
   "short_des":"JUST F.A.S.T(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();?>

在我的 php 脚本的评论部分是从网上复制的整个 json 字符串。 我面临的问题是,当我在 file_get_contents('http:/www.isoivit.in/json/Details.json') 返回的字符串上使用 json_decode 时,它 returns NULL,而当我在 json_decode 上使用从同一个 json 文件复制的字符串我得到了我想要的有效输出。这意味着我的 php 代码能够从网络读取 json 文件但无法对其进行解码。我收到 json 最后一个错误 4(语法错误)。虽然我已经在我的 android 应用程序中检查了所有错误及其 运行 是否可行。请帮忙。

您在 http:// 中只有一个 /,您应该更改为 http://www.isoivit.in/json/Details.json,并且由于 PC\'s 中的 \' 而得到 NULL(它使用简单的未转义 ' 进行解码,然后与 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);