Solved

Spliting a String

Posted on 2013-01-14
6
222 Views
Last Modified: 2013-01-15
Hi Folks

I have the following string

var data = "{"CTS":"0,51,52","lastTopic":53,"questions_answered":["3","2","4","1","9"],"scoreArray":[],"06_01":20}"

Open in new window


How do I get the value of 06_01 which is 20

I need the value of 06_01 (which in this case is 20)

Thanks

RG
0
Comment
Question by:rgarimella
  • 4
  • 2
6 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 38776837
var data = '{"CTS":"0,51,52","lastTopic":53,"questions_answered":["3","2","4","1","9"],"scoreArray":[],"06_01":20}';

// work on modern browser, check this page : http://caniuse.com/#search=JSON.parse
// for old browser download and include the following javascript in your page :
// https://github.com/douglascrockford/JSON-js/blob/master/
// <script type="text/javascript" src="path/to/your/js/folder/json_parse.js" ></script>
var json = JSON.parse(data); 

alert( json["06_01"] );

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 38776858
0
 

Author Comment

by:rgarimella
ID: 38779126
How would I code for older browsers especially IE7 ?

It works for the rest
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 82

Expert Comment

by:leakim971
ID: 38779152
read the code in my first post, line 4,5,6...
0
 

Author Comment

by:rgarimella
ID: 38779392
Sorry, I read the above lines for older browser. Confusion was in the json_parse.js, the sample code was like this

myData = json_parse(text, function (key, value) {..

Open in new window


your code was

var json = JSON.parse(data); 

Open in new window


Also do I have to detect older browsers ?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 38779500
Also do I have to detect older browsers ?

no, just add the script if you want to support them and use :

JSON = JSON?{parse:json_parse}:JSON;
var json = JSON.parse(data);
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question