Home Ask Login Register

Developers Planet

Your answer is one click away!

Jimmy Wijaya February 2016

How to call json from string

i have ajax function then i need call json from named string, how to deal with that?

$.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);
           }

        }

and my json like this :

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"

my question is how to call data.article-users_id because i created it dynamicly... thank you

Answers


Jimmy Wijaya February 2016

after searching i found how to call it like array data['string']

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


Matías Fidemraizer February 2016

You can't navigate object properties this way:

obj["property1.property2.property3"]

...but you need to do this way:

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

Post Status

Asked in February 2016
Viewed 1,301 times
Voted 8
Answered 2 times

Search




Leave an answer


Quote of the day: live life