如何从JSON响应中读取数据

时间:2015-08-14 03:30:21

标签: javascript php json laravel laravel-5

我目前正在使用Laravel 5。 在我的GoalsController中:

public function getGoalData()
{
    // my queries
    return view('userPages.usercp')->with('goal_data', $goal_data);
}

当我按$goal_data打印<?php echo $goal_data ?>时,它是一个json字符串。 现在我想通过使用Javascript进一步处理来阅读它。我怎样才能做到这一点? 谢谢。

编辑

我想在外部脚本中读取@goal_data。以下是Julian Paolo DayagVMcreator的解决方案。

  • 在您的HTML文件中,将其放在最后(</body>标记之前):

<script> window.GOALS = <?echo json_encode($goal_data); ?> </script>

根据Julian Paolo Dayag的说法:

  

通过将变量存储到窗口对象中,意味着它将变为a   全局变量。

  • 现在在您的JS文件中,您可以这样调用它:var goalData = JSON.parse(GOALS);

3 个答案:

答案 0 :(得分:0)

在javascript中使用JSON.parse将JSON字符串解析为Object。

JSON.parse(text[, reviver])

请在此处阅读:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

编辑

你可以这样做:

<script>
     var goal_data=JSON.parse(<?php echo $goal_data ?>);
</script>

如果它在laravel的刀片中,你可以使用JSON.parse({{ $goal_data }});

修改

我尝试了这样的示例JSON字符串:

<?php 
$goal='[
    {
        "goalId":28,
        "title":"1st goal",
        "description":"what the fuck",
        "startTime":"00:00:00",
        "endTime":"22:00:00",
        "remind":0,
        "autocheck":1,
        "repeat":"once",
        "goalStatus":"success",
        "type":"daily",
        "freqYear":0,
        "start":"08\/13\/2015",
        "end":""
    },
    {
        "goalId":29,
        "title":"2nd goal",
        "description":"Lorem ipsum dolor sit amet",
        "startTime":"17:25:00",
        "endTime":"17:25:00",
        "remind":1,
        "autocheck":0,
        "repeat":"once",
        "goalStatus":"success",
        "type":"daily",
        "freqYear":0,
        "start":"08\/13\/2015",
        "end":""
    },
    {
        "goalId":30,
        "title":"3rd goal",
        "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
        "startTime":"05:00:00",
        "endTime":"14:00:00",
        "remind":1,
        "autocheck":1,
        "repeat":"until",
        "goalStatus":"success",
        "type":"daily",
        "freqYear":0,
        "start":"08\/13\/2015",
        "end":"08\/19\/2015"
    },
    {
        "goalId":31,
        "title":"4th goal",
        "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
        "startTime":"00:00:00",
        "endTime":"16:00:00",
        "remind":0,
        "autocheck":1,
        "repeat":"once",
        "goalStatus":"success",
        "type":"weekly",
        "freqYear":2015,
        "start":"1",
        "end":""
    },
    {
        "goalId":32,
        "title":"5th goal",
        "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
        "startTime":"00:00:00",
        "endTime":"00:00:00",
        "remind":1,
        "autocheck":1,
        "repeat":"until",
        "goalStatus":"success",
        "type":"weekly",
        "freqYear":2015,
        "start":"9",
        "end":"14"
    },
    {
        "goalId":33,
        "title":"6th goal",
        "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
        "startTime":"00:00:00",
        "endTime":"00:00:00",
        "remind":1,
        "autocheck":1,
        "repeat":"once",
        "goalStatus":"success",
        "type":"monthly",
        "freqYear":2018,
        "start":"8",
        "end":""
    },
    {
        "goalId":34,
        "title":"7th goal",
        "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
        "startTime":"00:00:00",
        "endTime":"20:00:00",
        "remind":0,
        "autocheck":1,
        "repeat":"until",
        "goalStatus":"success",
        "type":"yearly",
        "freqYear":0,
        "start":"2015",
        "end":"2029"
    }
]';
?>
<script type="text/javascript">
    console.log(JSON.parse(<?php echo  json_encode($goal) ?>));
</script>

没有错误。

修改

如果你想将你的php变量传递给外部JS,你可以将它分配给全局变量,在你的head标签中,你的代码应该是这样的:

..assuming this is your head tag
<script>
    var goal_string=<?php echo json_encode($goal_data); ?>;
</script>
<script src="external_js.js" ></script>

现在您可以访问external_js.js上的var goal_string以及其声明下面包含的任何JS。

答案 1 :(得分:0)

您可以使用JSON.parse("json string here");

示例:

单个对象

    var person = JSON.parse("{name: 'hello world', age: 18}");

    console.log(person.name); // outputs "hello world"
    console.log(person.age);  // outputs 18

对象数组

    var person = JSON.parse("[{name: 'hello world', age: 18},{name: 'foo bar', age: 10}]");

    console.log(person[0].name); // outputs "hello world"
    console.log(person[0].age);  // outputs 18
    console.log(person[1].name); // outputs "foo bar"
    console.log(person[1].age);  // outputs 10

有时 JSON字符串出错,导致 JSON.parse()在解析失败时抛出异常。

建议将 JSON.parse()包装在 try / catch块周围,以便javascript不会停止执行您的脚本。

    try {
        var person = JSON.parse("[{name: 'hello world', age: 18},{name: 'foo bar', age: 10}]");

        console.log(person[0].name); // outputs "hello world"
        console.log(person[0].age);  // outputs 18
        console.log(person[1].name); // outputs "foo bar"
        console.log(person[1].age);  // outputs 10
    } catch(e) {
        console.log("JSON parse failure!");
    }

答案 2 :(得分:0)

有各种方法可以使用JS: -

var person = JSON.parse("[{JSON1: 'Value1', JSON2: value2},{JSON1: 'Value1', JSON2: Value2}]");
console.log(person[0].JSON1);
console.log(person[0].JSON2);
alert(person[0].JSON1);
alert(person[0].JSON2);

使用Java代码:

 String s = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
 Object obj = parser.parse(s);
 JSONArray array = (JSONArray)obj;
 System.out.println(array.get(1));

或在脚本标记内的PHP访问文件中: -

 var goal_data=JSON.parse("<?php echo $goal_data ?>");