0

What is wrong with my code below? I am passing to this function a JSON string but the assignment ends up blank. I can see the string in FireBug in the function parameter so I know it arrives at the function, but it does not make it past the assignment (as in the assignment is "undefined"). I want to pull from the string the wty_upgrade array. I am using some of the code I received help with in this question. Thanks

JSON string (as I see it in FireBug:

{"wty_upgrade":[
    {"wty":"Upgrade from 3 to 5 years", "mth":24, "pig":3000}, 
    {"wty":"Upgrade from 3 to 10 years", "mth":84, "pig":8000}
]}

Function Call:

LoadWtyUpgPlans('3', JSON)

function LoadWtyUpgPlans(StdWty, UpgWty) {
    var WtyRow = '';
    var WtyUpgrades = UpgWty.wty_upgrade;    <--- here the assignment is blank/undefined
    STUFF HERE...
};
5
  • Is the first part meant to be JavaScript code, or....? If you're just quoting your JSON, remove the JSON = " at the beginning and the " at the end (e.g., as in the question you linked). Commented Sep 20, 2014 at 7:31
  • Hi @T.J. Crowder, you are correct it is not code. I get the value from a MySQL tabke field. Is the syntax of the JSON incorrect somehow? Commented Sep 20, 2014 at 7:38
  • @TheRealPapa can you show how you output the JSON to the page after you've retrieved it from the database? Commented Sep 20, 2014 at 7:42
  • Hi @T.J. Crowder, apologies again, I should have removed the ". I even checked the validity of JSON syntax here http://jsonlint.com/ and it is OK (without the outside "). Commented Sep 20, 2014 at 7:43
  • if I alert(JSON) it looks exactly as above. If I alert(JSON.wty_upgrade) it shows up undefined Commented Sep 20, 2014 at 7:45

1 Answer 1

0
you can use as a this example may be useful fir you,
var jsn = '{"wty_upgrade":[
{"wty":"Upgrade from 3 to 5 years", "mth":24, "pig":3000}, 
{"wty":"Upgrade from 3 to 10 years", "mth":84, "pig":8000}]}';


 //OR you can parse then use
jsonres = JSON.parse(jsn); 
Sign up to request clarification or add additional context in comments.

1 Comment

It seems to be working after JSON.parse now. I do not understand why it needs to, but it works. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.