1

所以我在一个 php 文件中有大量数据,但它是 json 格式的。

我尝试将其转换为将所有 json 放入一个 $string 变量。然后:

$json = json_decode($string);

foreach($json as $key => $value) {
  echo $value;
}

虽然这不起作用,所以我正在考虑如何将所有这些数据放入 mysql 数据库(或数组)中。

这是数据的一小部分。

[{
"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"
}]
4

2 回答 2

3

这完全取决于确切的 json,但是您的示例代码会生成一个对象数组,这就是为什么echo不起作用。

您的示例应该适用于以下内容:

$json = json_decode($string);

foreach($json as $key => $value) {
  echo $value->namn;
}
于 2013-06-21T01:05:46.833 回答
1

这个怎么样:

$json = json_decode($string, true);

这应该是$json一个关联数组。

于 2013-06-21T01:05:24.473 回答