user2070749
user2070749

Reputation: 59

json to select option jquery

when i try to parsing my json data... i get this...

[object object],[object object],[object object]....

My code is like this...

var json
            function go(){
                json = jQuery.parseJSON($('#json').val());
                alert(json);

            }

And my json is look like this...

[{"Jenis Bahan":"sp100roll","Lebar":"160","Harga / Meter":"4300"},{"Jenis Bahan":"sp100ecer","Lebar":"160","Harga / Meter":"5300"},{"Jenis Bahan":"sp75ecer","Lebar":"160","Harga / Meter":"4300"},{"Jenis Bahan":"sp75roll","Lebar":"160","Harga / Meter":"3800"},{"Jenis Bahan":"sp50roll","Lebar":"160","Harga / Meter":"2800"},{"Jenis Bahan":"a","Lebar":"b","Harga / Meter":"c"},{"Jenis Bahan":"a","Lebar":"b","Harga / Meter":"c"},{"Jenis Bahan":"a","Lebar":"b","Harga / Meter":"c"}]

And my question is.. how to get a alert result like this...

Name = sp100roll value = 160-4300
Name = sp75ecer value= 160-5300
......

But the final is.. i would like to create a select box option from it.. so it would be like this...

<option value="160-4300">sp100roll</option>
<option value="160-5300">sp75ecer</option>
and etc...

Any one can help?

i already try many answer but still didnt work..

Upvotes: 0

Views: 1150

Answers (3)

V31
V31

Reputation: 7666

Use JSON.stringify instead of parseJSON. That might help

Upvotes: 0

lee tan
lee tan

Reputation: 133

That you json looks a bit problem,when you write like this:"Jenis Bahan":"sp100roll",it's equivalent to:var Jenis Bahan="sp100roll";So that the definition of variables is not consistent with the rules,you can change the json like this:[{"Jenis_Bahan":"sp100roll","Lebar":"160","Harga_Meter":"4300"},{"Jenis_Bahan":"sp100ecer","Lebar":"160","Harga_Meter":"5300"},{"Jenis_Bahan":"sp75ecer","Lebar":"160","Harga_Meter":"4300"},{"Jenis_Bahan":"sp75roll","Lebar":"160","Harga_Meter":"3800"},{"Jenis_Bahan":"sp50roll","Lebar":"160","Harga_Meter":"2800"},{"Jenis_Bahan":"a","Lebar":"b","Harga_Meter":"c"},{"Jenis_Bahan":"a","Lebar":"b","Harga_Meter":"c"},{"Jenis_Bahan":"a","Lebar":"b","Harga_Meter":"c"}] Now you can read it like this: function go(){ var data=[{"Jenis_Bahan":"sp100roll","Lebar":"160","Harga_Meter":"4300"},{"Jenis_Bahan":"sp100ecer","Lebar":"160","Harga_Meter":"5300"},{"Jenis_Bahan":"sp75ecer","Lebar":"160","Harga_Meter":"4300"},{"Jenis_Bahan":"sp75roll","Lebar":"160","Harga_Meter":"3800"},{"Jenis_Bahan":"sp50roll","Lebar":"160","Harga_Meter":"2800"},{"Jenis_Bahan":"a","Lebar":"b","Harga_Meter":"c"},{"Jenis_Bahan":"a","Lebar":"b","Harga_Meter":"c"},{"Jenis_Bahan":"a","Lebar":"b","Harga_Meter":"c"}]; for(var i=0;i

Upvotes: 0

Musa
Musa

Reputation: 97727

Don't use alerts to see the contents of variable use console.log. This will show you all the elements in your array and all the properties in the objects in the console, rather than just trying to convert it to a string like with alert.

json = jQuery.parseJSON($('#json').val());
console.log(json);
//console.dir(json);//for IE

http://jsfiddle.net/mowglisanu/zA54W/

For building the select just iterate through the array and set the values and text fron each object

...
$('#theselect').append('<option value="'+json[i]['Lebar']+'-'+json[i]['Harga / Meter']+
'">'+json[i]['Jenis Bahan']+'</option>');
...

Upvotes: 2

Related Questions