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).

6
  • will u always have something like this structure? Commented Jul 25, 2013 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. Commented Jul 25, 2013 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? Commented Jul 25, 2013 at 18:54
  • Thanks for all your responses. The answer below did it. @Felix King: I guess that's why the answer to stackoverflow.com/questions/4935632/… 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. Commented Jul 25, 2013 at 20:17
  • 1
    There is no such thing as a "JSON object". 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. Commented Jul 25, 2013 at 20:30

2 Answers 2

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) ); 
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent! Thanks for the help! This does it, pretty much. There's always the devil in the details....
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);

2 Comments

When I do this the first line returns a null.
oh, sorry it didn't help. you should post your code then, i'm using it in my project in the same condition as you (encoded in PHP, decoded in javascript) and it works like a charm.

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.