0

This is my first question to StackOverflow. I think answer is not so complicate, but I am very new to Javascript.

I have a JQuery AJAX function that parses this JSON object:

{
  "Users":[
    {
      "key":"1",
      "label":"Tom Clancy"
    },
    {
      "key":"12",
      "label":"Steve Martin"
    }
  ]
}

and should obtain the same result as:

var sections = [{
    key: 1,
    label: "Tom Clancy"
  }, {
    key: 12,
    label: "Steve Martin"
  }
];

I'm able to iterate through JSON element, but i don't know how to go on.

Can you provide suggestions?

EDIT: i can't still get it work...this is my code:

    var sections=[
        {key:1, label:"Section A"},
        {key:2, label:"Section B"},
        {key:3, label:"Section C"},
        {key:4, label:"Section D"}
    ];
    $.ajax({
    url: '/WebOffice/ListaUtenti',
    type: "POST",
    dataType: 'json',
    success: function (data) 
    {
        console.log( "success" );
        sections = data.Users;
    }});

    scheduler.createTimelineView({
        name:   "matrix",
        x_unit: "day",
        x_date: "%d %M",
        x_step: 1,
        x_size: 15,
        y_unit: sections,
        y_property: "section_id"
    });

The jquery ajax call doesn't assign the new value to sections (the call state is success, verified) and so scheduler still shows the original sections. Where i'm wrong?

thanks

6
  • 9
    var sections = json_object.Users? Commented Apr 6, 2013 at 19:27
  • To convert it to array you'd do var arr = $.makeArray(object), but there's really no reason to as you can access the object just as easily. Commented Apr 6, 2013 at 19:28
  • First I would take a look at an article on associative arrays - if youre a beginner this one will sufffice - blog.xkoder.com/2008/07/10/… You should also post the code you have, then I/others will edit/post our answers and help you fix what you have Commented Apr 6, 2013 at 19:29
  • So simple?? I'm really very new to javascript...thank you very much! Commented Apr 6, 2013 at 19:30
  • That xkoder page is a terrible article, full of misconceptions and bad practices. I don't have time to explain the details right now, but will follow up later if anyone is interested. In the meantime: Not recommended. Commented Apr 6, 2013 at 20:07

1 Answer 1

1

I will explain you the process. Go to any online JSON formatter, may be this one and pretty print your JSON. It will appear as.

{
   "Users":[
      {
         "key":"1",
         "label":"Tom Clancy"
      },
      {
         "key":"12",
         "label":"Steve Martin"
      }
   ]
}

So Users is an array of objects. Users[0] is first object and Users[1] is second object. So you can iterate the JSON easily and obtain the result you want.

Live demo : http://jsfiddle.net/sbymr/

See the console for the output.

Sign up to request clarification or add additional context in comments.

1 Comment

Users[0] is not an array...it is an object with properties key and label

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.