Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to parse a JSON string 1

Status
Not open for further replies.

varocho

Programmer
Dec 4, 2000
238
US
I have a JSP page that calls a Web service, and a JSON string is returned, which looks like this:

{"GetCustomerInfo": {"val": "Joe Schmoe"}}

How can I get the value of "val", using JavaScript?
 
Hi

JavaScript:
str[teal]=[/teal][green][i]'{"GetCustomerInfo": {"val": "Joe Schmoe"}}'[/i][/green]

[COLOR=darkgoldenrod]eval[/color][teal]([/teal][green][i]'obj='[/i][/green][teal]+[/teal]str[teal])[/teal]    [gray]// any browser[/gray]

obj[teal]=[/teal]JSON[teal].[/teal][COLOR=darkgoldenrod]parse[/color][teal]([/teal]str[teal])[/teal] [gray]// Gecko 1.9.1 ( Firefox 3.5, SeaMonkey 2, Thunderbird 3 )[/gray]

[COLOR=darkgoldenrod]alert[/color][teal]([/teal]obj[teal][[/teal][green][i]'GetCustomerInfo'[/i][/green][teal]][[/teal][green][i]'val'[/i][/green][teal]])[/teal]
Note that some people considers the use of [tt]eval()[/tt] insecure and suggest the use of a parser. You can find such code on json.org.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top