2

I have a Json file

{
   "info":[
      {
         "Name":"Noob Here1",
         "Department":"Language",
         "Sex":"Male",
         "Basic_Salary":"175",
         "ESI":"58",
         "Employee_PF":"60.50",
         "Bonus":"2.60"
      },
      {
         "Name":"Noob Here2",
         "Department":"Employee_PF",
         "Sex":"Female",
         "Basic_Salary":"10.5",
         "ESI":"4.0",
         "Employee_PF":"20",
         "Bonus":"0.5"
      },
      {
         "Name":"Noob Here3",
         "Department":"Physics",
         "Sex":"Male",
         "Basic_Salary":"165",
         "ESI":"55",
         "Employee_PF":"875",
         "Bonus":"200"
      }
   ]
}

And I take data from it using getJson

loadData(myFile).done(function (data1) {

    // check if we acutally get something back and if the data has the info property
    if (data1 && data1.info) {
        data = data1;
        var html = "";
        var head = "";
        $.each(data1.info[0], function (key, value) {
            html += "<td><input data-bind='value:" + key + "'></td>";
            head += "<th>" + key + "</th>";
        });
        html += "<td><input data-bind='value: result'/></td>";
        head += "<th>Result</th>";
        $("#div1 thead").append(head);
        $("#div1").append("<tr></tr>");
        $("#div1 tr").append(html);
    }
});

Note: myFile attribute gives the name of JSON file

and append data to DOM

Can i Use a JSON like

{
   [
      {
         "Name":"Noob Here1",
         "Department":"Language",
         "Sex":"Male",
         "Basic_Salary":"175",
         "ESI":"58",
         "Employee_PF":"60.50",
         "Bonus":"2.60"
      },
      {
         "Name":"Noob Here2",
         "Department":"Employee_PF",
         "Sex":"Female",
         "Basic_Salary":"10.5",
         "ESI":"4.0",
         "Employee_PF":"20",
         "Bonus":"0.5"
      },
      {
         "Name":"Noob Here3",
         "Department":"Physics",
         "Sex":"Male",
         "Basic_Salary":"165",
         "ESI":"55",
         "Employee_PF":"875",
         "Bonus":"200"
      }
   ]
}

If so how to call it?

7
  • data1[0][0] gives Noob here1 Commented Apr 4, 2013 at 12:38
  • remove .info and proceed with data as it is Commented Apr 4, 2013 at 12:38
  • Take a look here: stackoverflow.com/questions/6964387/… Commented Apr 4, 2013 at 12:38
  • You can use a for statement to loop over the array Commented Apr 4, 2013 at 12:39
  • 2
    The second JSON example isn't valid, as object elements must be named. Commented Apr 4, 2013 at 12:43

1 Answer 1

5

Use JSONLint or JSLint to see if your syntax is correct, and you will get this:

Parse error on line 1:
{    [        {        
-----^
Expecting 'STRING', '}'

While it may be possible to parse it anyway, you should never ever do this.

As noted by Marco S. in the comments, you can instead use this:

[
    {
         "Name":"Noob Here1",
         "Department":"Language",
         "Sex":"Male",
         "Basic_Salary":"175",
         "ESI":"58",
         "Employee_PF":"60.50",
         "Bonus":"2.60"
     },
     ...
]

And access it like

result[0]["Name"]
Sign up to request clarification or add additional context in comments.

Comments

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.