0

I have a .json file uploaded on my web server with valid syntax.( http://www.isoivit.in/Details.json).

I want to edit it using php. My php code is as given below:

<?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\[email protected] \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\[email protected]\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\[email protected]\r\nAnkush Saini\r\[email protected]\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\[email protected] \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\[email protected]\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\[email protected]\r\nAnkush Saini\r\[email protected]\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. 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. This means that my php code is able to read json file from web but unable to decode it. I get a json last error 4(SYNTAX ERROR). Though I have checked for all errors and its running feasibly on my android application. Please help.

3
  • http:/www.isoivit.in/json/Details.json it's only with a slash / .. should be http://www.isoivit.in/json/Details.json Commented Jan 19, 2015 at 10:50
  • If you get a file with a .json extension, why are you calling preg_replace on its contents, still? I mean, why not json_decode it straight away? Calling preg_replace is just pointless, and might be the cause of the data corruption Commented Jan 19, 2015 at 10:51
  • Its a typing error. I am redirecting to this url only "isoivit.in/json/Details.json". And reason of using preg_replace is that I read some solutions from web which suggested me to remove extra spaces. new lines and tabs. Still not working. Commented Jan 19, 2015 at 11:28

1 Answer 1

1

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:

$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);
Sign up to request clarification or add additional context in comments.

1 Comment

You saved my job dude. Thanks a lot. I was messing with this around 15 days and finding irrelevant solutions. Salute!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.