如何从字符串中调用json

时间:2016-02-06 20:36:25

标签: javascript jquery json

我有ajax函数然后我需要从命名字符串调用json,如何处理?

$.ajax({
  url: fullUri + curID,
  type: "get",
  dataType: 'json',
  success: function (data) {
     $.each(data.data, function (key, value) {
        var typeInput = data.fields[key];
        if (typeInput == 'select'){
           var listJson = 'data.'+uri+'-'+key;
           $.each(listJson,function(key2,value2){
              alert(value2);
           }

        }

和我的json喜欢这样:

data:
    data: 
       articles_title: "title",
       articles_content: "content"
       users_id: 1
    fields:
       articles_content: "textarea"
       articles_title: "text"
       users_id: "select"
    article-users_id:
       field: "value"
       field2: "value2"

我的问题是如何调用data.article-users_id,因为我动态地创建了它...谢谢

2 个答案:

答案 0 :(得分:0)

您无法通过这种方式导航对象属性:

obj["property1.property2.property3"]

...但你需要这样做:

obj["property1"]["property2"]["property3"]

答案 1 :(得分:-1)

搜索后我发现如何将其称为数组数据[' string']

       var listJson = 'data.'+uri+'-'+key;
       $.each(data[listJson],function(key2,value2){
          alert(value2);
       }