0

On selecting JSON object from dropdown list the values releted to that object should display in body or textbox.

Here is my JSON demo:

         {  
    "EmployeeDetails":[  
      {  
         "EmpID":1,
         "EmpName":"xyz",
         "EmpPhn":231
      },
      {  
         "EmpID":1,
         "EmpName":"xyz",
         "EmpPhn":231
      }
    ],
    "jobDetails":{  
      "JobID":1,
      "JobName":"opqr"
    },
    "DeptDetails":{  
      "deptID":1,
      "deptname":"qtrye",
      "deptloc":"xbnz"
      } 
    }

Example if I select "JobDetails" from droplist then i should display

     JobID: 1 
     JobName: opqr 

in body content.

6
  • can you provide jsfiddle? Commented Sep 15, 2014 at 7:50
  • Here is code for selection jsfiddle.net/TY76W/23 Commented Sep 15, 2014 at 7:58
  • Sorry Bro, Im new in jsfiddle so im not aware how to add the data.json file in that. Commented Sep 15, 2014 at 8:00
  • 1
    jsfiddle.net/TY76W/24 Commented Sep 15, 2014 at 8:04
  • Thanks Mohit Arora, its coming Perfect. Commented Sep 15, 2014 at 9:12

1 Answer 1

2

You can create a select element with options, which values are the keys of your JSON data. Then on change event, display the value of the specified key.

I have implemented the mentioned solution:

HTML

<select id="dropdown">
    <option value="EmployeeDetails">Employee Details</option>
    <option value="jobDetails">Job Details</option>
    <option value="DeptDetails">Dept Details</option>
</select>

<pre id="objView"></pre>

JavaScript

var json = {
    "EmployeeDetails": [{
        "EmpID": 1,
        "EmpName": "xyz",
        "EmpPhn": 231
    }, {
        "EmpID": 1,
        "EmpName": "xyz",
        "EmpPhn": 231
    }],
        "jobDetails": {
        "JobID": 1,
            "JobName": "opqr"
    },
        "DeptDetails": {
        "deptID": 1,
        "deptname": "qtrye",
        "deptloc": "xbnz"
    }
};

var $dropdown = $('#dropdown'),
    $objView = $('#objView');

$dropdown.on('change', function() {
    $objView.html(JSON.stringify(json[$dropdown.val()]));
});

$dropdown.trigger('change');

And here is the JSFiddle.

Note: I print the plain object in pre tag. You can prettify and do whatever you need before outputing in HTML.

UPDATE

I have updated the solution. Instead of having a static HTML, I have generated the options from the JSON. Here it is:

HTML

<select id="dropdown">
</select>

<pre id="objView"></pre>

JavaScript

var $dropdown = $('#dropdown'),
$objView = $('#objView'),
$docFragment = $(document.createDocumentFragment());

var json = {
    "EmployeeDetails": [{
        "EmpID": 1,
        "EmpName": "xyz",
        "EmpPhn": 231
    }, {
        "EmpID": 1,
        "EmpName": "xyz",
        "EmpPhn": 231
    }],
        "jobDetails": {
        "JobID": 1,
            "JobName": "opqr"
    },
        "DeptDetails": {
        "deptID": 1,
        "deptname": "qtrye",
        "deptloc": "xbnz"
    }
};

for (var prop in json) {
    $('<option/>', {
        val: prop,
        text: prop
    }).appendTo($docFragment);
}

$dropdown.append($docFragment);

$dropdown.on('change', function() {
    $objView.html(JSON.stringify(json[$dropdown.val()]));
});

$dropdown.trigger('change');

JSFiddle.

UPDATE 2 (regarding your comment)

Probably you have a few options here.

1.You can implement a function, which will take the object, iterate over the properties and nicely print them.

2.You can do the above using MV* framework, such as AngularJS. You just bind the model, set some value (e.g. object) and it will take care the iterating and related things for you. Finally, it will print the object in any form you like.

3.Finally, if you don't want to iterate over the properties or use a framework, you can simply remove the unwanted characters from the string. For example, this is code for removing curly/square brackets, double quotes as well as replacing the commas with new line:

$dropdown.on('change', function() {
    var objString = JSON.stringify(json[$dropdown.val()]);

    objString = objString.replace(/[\{\}\[\]\"]*/ig, '');    // Remove unwanted chars
    objString = objString.replace(/\,/ig, '<br/>');         // Replace commas with new lines (i.e. br tags)

    $objView.html(objString);
});

JSFiddle.

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

6 Comments

Thanks Karlen , iam using a data.json file which contains huge JSON object . For Every Json Object i cannot use <option> in <select>
Thanks Karlen Kishmiryan
Could you tell me how remove double qoutes , curly braces and squre bracket from the genrated result
Thanks Karlen Kishmiryan, Best Regards
Hi, Its was working fine before 2 hour ago but now an error is throwing in " $objView.html(JSON.stringify($dropdown.val()])); " specifying "jquery 0x800a1391 - javascript runtime error 'json' is undefined" when i execute in IE9. Could you help me out with these issue.
|

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.