javascript - How to extract JSON from URL? - Stack Overflow

I'm looking to extract JSON data from a server URL that looks something like this:The URL itself o

I'm looking to extract JSON data from a server URL that looks something like this:


The URL itself outputs the data like such:

[ 
  { "name": "Not configured", 
    "mac_address": "####c##c####", 
    "online": "false", 
    "rate": "Not configured"},

  { "name": "Not configured", 
    "mac_address": "####c##c####", 
   "online": "false", 
   "rate": "Not configured"},
]

The outputed data changes and will need to be updated accordingly. I'm wondering what is the best method to retrieve the JSON data from the URL and keep it updated? I've looked at the jQuery getJSON function, and I'm not sure that will work as the URL is simply outputting JSON, it's not a .json file.

Can what I described be done using JavaScript, jQuery or any other open methodology?

Thanks.

I'm looking to extract JSON data from a server URL that looks something like this:

https://dev.randomdomain./subdomain

The URL itself outputs the data like such:

[ 
  { "name": "Not configured", 
    "mac_address": "####c##c####", 
    "online": "false", 
    "rate": "Not configured"},

  { "name": "Not configured", 
    "mac_address": "####c##c####", 
   "online": "false", 
   "rate": "Not configured"},
]

The outputed data changes and will need to be updated accordingly. I'm wondering what is the best method to retrieve the JSON data from the URL and keep it updated? I've looked at the jQuery getJSON function, and I'm not sure that will work as the URL is simply outputting JSON, it's not a .json file.

Can what I described be done using JavaScript, jQuery or any other open methodology?

Thanks.

Share Improve this question asked Jun 22, 2011 at 16:31 TedTed 1232 gold badges2 silver badges4 bronze badges 1
  • Is this server on the same domain you're making a request from? If not, then $.getJSON won't work without using jsonp. – scurker Commented Jun 22, 2011 at 16:56
Add a ment  | 

6 Answers 6

Reset to default 1
$,getJSON('url',function(data){
     alert(data);
    // or alert("sample text"); would do if the json file is handled well enough.
    // if no alert is shown, it's either you're using chrome with an offline version of 
    // your project or you're having an invalid json file
});

May I ask what browser are you using? $.getJSON won't work in Chrome if you are doing it in offline mode.

I also got frustrated why the json file not being processed even if I don't use Chrome - e.g. i use FF or IE.

I discovered that you shouldn't put ments inside the json file as it makes it invalid. I think, it's because .getJSON really parses data inside the JSON file as text, not as a javascript file, so it will also run through the ment (if you have any), and for sure won't understand it, so I suggest you remove ment block inside your json files.

Have you even tried using $.getJSON()? It does exactly what you need it to do, it simply reads the response from the server and returns a json object.

It is not very clear from your question if you want to process the data on client side (browser etc.) or on server side for offline processing.

For accessing and processing this data on server side, you can use curl/libcurl to pull data and decode it to an object in language of your choice. For example in php:

<?php 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://dev.randomdomain./subdomain");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

data_as_php_object = json_decode($output);
?>

Firstly, you're actually trying to parse JSON from a response. The url is the thing you request a response from. Here is a simple example using getJSON:

$.getJSON('https://dev.randomdomain./subdomain', function(data) {
    var output = "";
    $.each(data, function(index, node) {
        output += node.name + ": " + node.mac_address + "<br/>";
    });
    document.write(output);
});

http://jsfiddle/YwPzW/1/

The function $.getJSON() is to put the string in JSON format into a javascript object. So if you want to get the data, you can try as:

$.getJSON("https://dev.randomdomain./subdomain",function(data){
   alert(data[0].name);
});

Do you trust the source of the JSON string. If you do, just use

alert(eval(dropStringHere));

The getJSON jQuery method should also work fine.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744845857a4596831.html

相关推荐

  • javascript - How to extract JSON from URL? - Stack Overflow

    I'm looking to extract JSON data from a server URL that looks something like this:The URL itself o

    2天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信