从JSON字符串中获取特定值

时间:2018-02-04 15:14:42

标签: json ajax

我正试图从我的json字符串中获取一个简单的值。它似乎比我想象的更难。我如何在以下JSON中找到第一个ModuleAction对象的'name'(第一个数组称为'data'):

[
   {
      "ModuleController":{
         "id":"3"
      },
      "ModuleActionModuleController":[
         {
            "id":"4",
            "module_action_id":"1",
            "module_controller_id":"3",
            "ModuleAction":{
               "id":"1",
               "name":"Overzicht",
               "action":"index"
            }
         },
         {
            "id":"5",
            "module_action_id":"2",
            "module_controller_id":"3",
            "ModuleAction":{
               "id":"2",
               "name":"Detail",
               "action":"view"
            }
         }
      ]
   }
]

这是我最好的尝试:

data[0].ModuleActionModuleController[0].id

但我有错误:

add:93 Uncaught TypeError: Cannot read property '0' of undefined
    at Object.success (add:93)
    at j (jquery-2.1.0.min.js:2)
    at Object.fireWith [as resolveWith] (jquery-2.1.0.min.js:2)
    at x (jquery-2.1.0.min.js:4)
    at XMLHttpRequest.<anonymous> (jquery-2.1.0.min.js:4)

知道我做错了什么吗? :)

修改

这是返回数据JSON字符串的ajax函数:

$(function() {
    $('#PageModuleId').on('change', function(){
        var formData = $('#PageAddForm').serialize();

        // POST to server

        $.ajax({
            type: 'POST',
            url: '<?php echo $this->here; ?>',
            dataType: 'text',
            data: formData,
            success: function(data){
                console.log(data[0].ModuleActionModuleController[0].ModuleAction.name);
            }
        });
    });
});

2 个答案:

答案 0 :(得分:1)

  

我如何才能找到例如&#39; 名称&#39;第一个ModuleAction   以下JSON中的对象?

在我看来,你只是错过了下一个子元素:ModuleActionModuleController.ModuleAction.name

&#13;
&#13;
$(document).ready(function() {
var obj = jQuery.parseJSON( '{"ModuleController":{"id":"3"},"ModuleActionModuleController":[{"id":"4","module_action_id":"1","module_controller_id":"3","ModuleAction":{"id":"1","name":"Overzicht","action":"index"}},{"id":"5","module_action_id":"2","module_controller_id":"3","ModuleAction":{"id":"2","name":"Detail","action":"view"}}]}' );
//alert( obj.ModuleActionModuleController[0].ModuleAction.name );
document.body.innerHTML = obj.ModuleActionModuleController[0].ModuleAction.name;
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这对我有用:

&#13;
&#13;
var JSON = {
    "data": [
   {
      "ModuleController":{
         "id":"3"
      },
      "ModuleActionModuleController":[
         {
            "id":"4",
            "module_action_id":"1",
            "module_controller_id":"3",
            "ModuleAction":{
               "id":"1",
               "name":"Overzicht",
               "action":"index"
            }
         },
         {
            "id":"5",
            "module_action_id":"2",
            "module_controller_id":"3",
            "ModuleAction":{
               "id":"2",
               "name":"Detail",
               "action":"view"
            }
         }
      ]
   }
]
};

 alert(JSON.data[0].ModuleActionModuleController[0].ModuleAction.id);
&#13;
&#13;
&#13;