如何在JavaScript中解析JSON对象

时间:2016-05-21 05:27:03

标签: javascript json parsing

我有以下功能,它当前只显示从GET请求返回到控制台的对象。我需要做的是解析“数据”对象,以便我可以将“输出”字符串从数据返回到我的网页。任何帮助将不胜感激。

window.onload = function() {
    var output = $.ajax({
        url: 'https://ajith-holy-bible.p.mashape.com/GetVerseOfaChapter?Book=John&chapter=3&Verse=16', // The URL to the API. You can get this by clicking on "Show CURL example" from an API profile
        type: 'GET', // The HTTP Method, can be GET POST PUT DELETE etc
        data: {}, // Additional parameters here
        dataType: 'json',
        success: function (data) {
            //
            //Change data.source to data.something , where something is whichever part of the object you want returned.
            //To see the whole object you can output it to your browser console using:
            console.log(data);
        },
        error: function (err) {
            alert(err);
        },
        beforeSend: function (xhr) {
            xhr.setRequestHeader("X-Mashape-Authorization", "my_key_here"); // Enter here your Mashape key
        }
    });
}

2 个答案:

答案 0 :(得分:0)

我不确定你的JSON是怎样的,但让我们说是

{
    "foo": "some value",
    "bar": "some other value"
}

然后你可以这样做:

success: function (data) {

    console.log(data);

    var dataObj = $.parseJSON(data);

    $.each(dataObj, function(key, value) {
        if(key == "foo") {
            // do something with foo's value
            console.log(value);         
        }
        if(key == "bar") {
            // do something with bar's value
            console.log(value); 
        }
    });

}

答案 1 :(得分:0)

如果

{
   foo:'bar'
}

,您的回复看起来像

    success: function (data) {

     console.log(data.foo)

您可以像

一样访问
"bar"

将输出

public function loadadd($mekhala_Id){

        $query = $this->db->get_where('tb_unit', array('mandalam_Id' => $mekhala_Id));
        echo  form_open('Payment/amount'); 
    ?>


<h1>Members List</h1>
        <table border="1">
            <tr>
                 <th>Unit</th>
                  <th>Unit Secretary</th>
                 <th>Amount paid</th>

            </tr>
            <?php
    foreach ($query->result() as $row)
    {
        ?>

            <tr>
                <td> <?php echo $row->unitName ;?></td>
                        <td> <?php echo $row->unit_sec ;?></td>
                       <td> <?php echo form_input(array('name'=>'na','placeholder'=>'Rupees Paid')) ;?></td>  
            </tr>
            <?php

}

 echo form_submit(array('name'=>'sub','value'=>'submit'));
        echo form_close();
}



 public function loadpayment($paid){

   //  $paid= $this->input->post('na');

           $fieldsData = $this->db->field_data('tb_unit');
           $datacc = array(); // you were setting this to a string to start with, which is bad

     foreach ($fieldsData as $key => $field)
{
    $datacc[ $field->Amount] = $this->input->post( $field->Amount);
}
$this->db->insert('tb_unit', $datacc);

}
   //  $this->db->set('Amount', $paid)




}




?> 

将请求分配给变量&#34;输出&#34;

也是不可靠的。

如果这对你不起作用你应该json-lint你的回答,也许标题是不正确的......等等。