0

I have a very simple question and am amazed I haven't figured it out yet. What am I missing? I get a php array through json encoding:

var curr_op = <?php echo json_encode($options) ?>;

The console.log(curr_op) reads:

[14:35:00.889] ({' ':" ", 'Tangential-S1':"Tangential-S1", 'Diagonal-Q1':"Diagonal-Q1", addnew:"Add New"})

and I would like to pass just the keys (or values) to html via:

$('#modal-body span2').html(JSON.stringify(curr_op));

which gives me an ugly thing:

{" ":" ","Tangential-S1":"Tangential-S1","Diagonal-Q1":"Diagonal-Q1","addnew":"Add New"}

in my webpage. How do I clean this up and only pass something like:

Tangential-S1, Diagonal-Q1

To the html (note the first and last elements should be removed too).

  • will u always have something like this structure? – krishwader Jul 25 '13 at 18:52
  • 1
    FYI, this has nothing to do with JSON. In your JavaScript script, `curr_op` is an object. But the bigger issue is: Object properties are not ordered. You cannot talk about the "last" or "first" property of an object. Different browser might iterate over them in different order. If you only want specific keys I suggest you create an array on the server side only containing those keys. – Felix Kling Jul 25 '13 at 18:52
  • If you know the names of the properties that you want to get rid of then there is a solution. But is that the case? Or asking in a different way, what "rule" do you want to use to filter the properties? – Ma3x Jul 25 '13 at 18:54
  • Thanks for all your responses. The answer below did it. @Felix King: I guess that's why the answer to http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript?rq=1 didn't work for me. Even though I'm json_encoding curr_op is not a json object? I'm still finding JSON, ajax, and jQuery to be quite tricky with all these data types etc. Hopefully it gets cleared with more use. – MysticalTautologist Jul 25 '13 at 20:17
  • 1
    [There is no such thing as a "JSON object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). Even though you use `json_decode` in your PHP script to serialize some data, in the JavaScript script you will end up having something like `var curr_op = {"foo": "bar"};` which is an *object literal* (which creates an object). JSON looks very similar to that (because its syntax was inspired by JS' object literal syntax) but its independent from JS. JSON is a *data-exchange format*, like XML or CSV. It's just a way to describe data as text. – Felix Kling Jul 25 '13 at 20:30
  • Thanks for the comment Felix. So json_decode is necessary to get the array from the php side, but it does not make a distinct kind of "object" per se. The analogy with XML and CSV helps. – MysticalTautologist Jul 26 '13 at 18:59

2 Answers2

1

Here's a fiddle to play with: http://jsfiddle.net/X8E9R/2/

function getPropsAsString(curr_op) {        
    var props = [];
    for (prop in curr_op) {
        var value = curr_op[prop];

        // filter them by some logic
        if (value === " " || value === "Add New")
            continue; // skip it

        props.push(prop);
    }

    // do whatever you want with the properties
    props.sort(); // maybe you want to sort them?

    return props.join(", ");
}

$('#modal-body span2').html( getPropsAsString(curr_op) ); 
Ma3x
  • 5,761
  • 2
  • 17
  • 22
0

if you want to decode the JSON in your javascript, write something like that :

var decoded_json = $.parseJSON(curr_op);    
$('#modal-body span2').html(decoded_json.Tangential-S1 + ', ' + decoded_json.Diagonal-Q);
Pascamel
  • 953
  • 6
  • 18
  • 28