Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-28-2012, 11:21 AM   PM User | #1
smching
New to the CF scene

 
Join Date: May 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
smching is an unknown quantity at this point
How to extract record from Json

According to this webpage: http://www.json.org/js.html
You can use myJSONObject.bindings[0].method to extract the following data

var myJSONObject = {"bindings": [
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};

I had an array which is generated from PHP & sqlite. The format will be similar as below (same as above data without bindings in front). How can I extract the following data?

var myJSONObject = {[
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};
smching is offline   Reply With Quote
Old 05-28-2012, 08:08 PM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by smching View Post
I had an array which is generated from PHP & sqlite. The format will be similar as below (same as above data without bindings in front). How can I extract the following data?

var myJSONObject = {[
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};
If that is code then you don't need to extract it to use it in JavaScript because it is JavaScript.

If it is a text string then you can convert it into a JavaScript object using JSON.parse().
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 05-31-2012, 03:10 PM   PM User | #3
smching
New to the CF scene

 
Join Date: May 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
smching is an unknown quantity at this point
Based on below string:
var myJSONObject = {[
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};

How to get deleteURI from method? JSON.parse() is not working with my browser. I'm using IE8 & Google Chrome 19.0.1084.52 + EasyPHP 5.3.9

Note: Maybe I'm using the wrong word (extract), it should be how to get record.

Last edited by smching; 05-31-2012 at 03:17 PM..
smching is offline   Reply With Quote
Old 05-31-2012, 03:33 PM   PM User | #4
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 806
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
var myJSONObject = {a:[
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};


alert(myJSONObject.a[1].method)

http://www.w3schools.com/json/default.asp
DaveyErwin is offline   Reply With Quote
Old 06-01-2012, 02:59 PM   PM User | #5
smching
New to the CF scene

 
Join Date: May 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
smching is an unknown quantity at this point
Quote:
Originally Posted by DaveyErwin View Post
var myJSONObject = {a:[
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};


alert(myJSONObject.a[1].method)

http://www.w3schools.com/json/default.asp
Hi, DaveyErwin
I know I can use myJSONObject.a[1].method to read the above Json format. But my format do not have a infront.
smching is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:17 AM.


Advertisement
Log in to turn off these ads.