So I've got this huge amount of data in a php-file but it is in json-format.
I have tried converting it putting all the json into one $string variable. Then:
$json = json_decode($string);
foreach($json as $key => $value) {
echo $value;
}
This doesn't work though so I'm woundering how I can put all this data into a mysql-database instead (or arrays).
This is a small part of the data.
[{
"namn":"ABF VUX",
"schoolID":"85740",
"stad":"G\u00f6teborg",
"PeriodDropDownList":false,
"FreeTextBox":false,
"code":"680378",
"lan":"V\u00e4stra G\u00f6talands l\u00e4n",
"WeekDropDownList":true,
"TypeDropDownList":true,
"startTid":"-"
},
{
"namn":"Adolf Fredriks Musikklasser",
"schoolID":"29320",
"stad":"Stockholm",
"PeriodDropDownList":true,
"FreeTextBox":true,
"code":"",
"lan":"Stockholms l\u00e4n",
"WeekDropDownList":true,
"TypeDropDownList":true,
"startTid":"8:15"
}]
-
how are you opening the file, show that part....Kylie– Kylie2013年06月21日 01:00:12 +00:00Commented Jun 21, 2013 at 1:00
-
"This doesn't work" -- well, what did you expect to happen, and what actually happened? Please provide enough info to actually answer the question.Hamish– Hamish2013年06月21日 01:01:19 +00:00Commented Jun 21, 2013 at 1:01
-
1also....if its php in the first place, doesn't that mean it was an array at one point in the cycle....surely before it gets encoded to json, you can upload? no? Am I missing something. Why have another file that decodes it and uploads?Kylie– Kylie2013年06月21日 01:01:28 +00:00Commented Jun 21, 2013 at 1:01
-
Also, is your question "how do I turn it into an array" (the title) or "how do I put it into mysql" (in the question)?Hamish– Hamish2013年06月21日 01:02:21 +00:00Commented Jun 21, 2013 at 1:02
2 Answers 2
It all depends on the exact json, but your example code generates an array of objects so that is why echo does not work.
What should work with your example, is something like:
$json = json_decode($string);
foreach($json as $key => $value) {
echo $value->namn;
}
answered Jun 21, 2013 at 1:05
jeroen
91.8k22 gold badges118 silver badges132 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
How about this:
$json = json_decode($string, true);
This should make $json an associative array.
answered Jun 21, 2013 at 1:05
jh314
27.9k16 gold badges66 silver badges83 bronze badges
Comments
lang-php