-1

say I want to parse an online file that has the following:

[{"guild": "Crimson", "region": "us", "realm": "Caelestrasz", "timestamp": 1311860040}, {"guild": "CrimsonR", "region": "us", "realm": "Caelestrasz", "timestamp": 1311511740}]

URL is: http://www.example.com

How would I go about in parsing certain information such as the first Guild, "Crimson"'s timestamp value?

Any help is appreciated :D

3 Answers3

1

you can use JSON.parse(). Its usage is simple:

var json = '{"result":true,"count":1}',
obj = JSON.parse(json);

alert(obj.count);

If you want to access array, you can simply do it like this:

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

This reference may be helpful : JSON in JavaScript

Ahsan Shah
  • 3,931
  • 1
  • 34
  • 48
0
var parsed = $.parseJSON( <input> );

then use:

parsed.guild 

parsed.timestamp
blorkfish
  • 21,800
  • 4
  • 33
  • 24
  • Honestly, this seems like a use of jQuery where it's clearly not necessary (because, as Ashan mentions, a simple ```JSON.parse()``` is sufficient). The question doesn't mention jQuery _at all_, so we should try to discourage this. – gtmtg Apr 20 '14 at 05:01
0

I write this for php. You can try this.

$curl = curl_init('http://www.example.com');// insert your desire url
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

$page = curl_exec($curl);

if (curl_errno($curl)) { // check for execution errors
echo 'Scraper error: ' . curl_error($curl);
exit;
}

curl_close($curl);
$allips = json_decode($page);

foreach ($allips as $datakey) {
$guild = $datakey->guild;
$timstamp = $datakey->timestamp;
echo 'Guild:'.$guild.'<br/> timstamp:'.$timstamp.'<br/><br/>';

}

And for javascript Parse JSON in JavaScript? this will help.

Thanx,

Community
  • 1
  • 1