javascript - 如何使用 javaScript 和 jQuery 从 URL 保存 json 对象

标签 javascript jquery json

<分区>

所以我有一个 php 页面,它以正确的格式返回一个 JSON 对象。喜欢:

  [ 
    {
    "name":"Users",
    "parent":"null",
    "children":[ {
        "name": "adsd", "parent": "Users", "children": []
    }
    ,
    {
        "name": "ca", "parent": "Users", "children": []
    }
    ,
    {
        "name":"ChanakaP",
        "parent":"Users",
        "children":[ {
            "name": "Carlos Puyol rejects Barcelona sporting director role", "parent": "ChanakaP"
        }
        ,
        {
            "name": "\r\nDiego Costa could leave Atletico Madrid", "parent": "ChanakaP"
        }
        ,
        {
            "name": "FIFA insist Lionel Messi's award for best men's player was not rigged", "parent": "ChanakaP"
        }
        ,
        {
            "name": "\r\nReal madrid interested in Van De Beek", "parent": "ChanakaP"
        }
        ,
        {
            "name": "Hazard scores debut goal", "parent": "ChanakaP"
        }
        ]
    }
    ,
    {
        "name": "dsd", "parent": "Users", "children": []
    }
    ,
    {
        "name": "ggggggggggggggggg", "parent": "Users", "children": []
    }
    ,
    {
        "name":"james123",
        "parent":"Users",
        "children":[ {
            "name": "first post", "parent": "james123"
        }
        ]
    }
    ,
    {
        "name": "new", "parent": "Users", "children": []
    }
    ,
    {
        "name": "new123", "parent": "Users", "children": []
    }
    ,
    {
        "name": "test", "parent": "Users", "children": []
    }
    ]
  }
]

我需要获取此 JSON 对象并将其以以下形式传输到我的脚本中的变量

var obj = k;

其中 k 是从此 URL 返回的 JSON 对象。我导入了 jQuery 并尝试了 $.getJSON 方法但没有成功。因此,如果您有一个带有 JSON 对象的 URL“ex.com”,我将如何将它原封不动地保存到一个变量中。因此,如果我将 url 的输出直接复制到一个变量中,例如:

var obj2 = copiedJSON;

我的函数按预期工作,但是当我像这样使用 $.getJSON 时:

$.getJSON('http://localhost/CS3744-N/GoalLiga/viz', function(json){
     obj2 = json;
});

我的函数返回一个错误。 JSON 函数是否更改返回的输出。

最佳答案

您没有发布您为 $.getJSON 尝试的代码,所以我无法告诉您为什么它被破坏了,但这里是您将如何使用 $.getJSON 来实现您的解决方案:

// You can change api.github.com to any URL you want and it should still work
$.getJSON("https://api.github.com", function(json){ 
    // This is the callback function. The variable json will only be avaliable within this function
    console.log(json); 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

请注意,json 变量仅在您提供给 $.getJSON 的函数中可用,这是因为在 GET 中请求是异步执行的(例如,您的其余代码在发出请求时保持运行),所以只需确保将要运行的任何依赖于 json 的代码都放在回调函数中。

关于javascript - 如何使用 javaScript 和 jQuery 从 URL 保存 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59341242/

相关文章:

javascript - 由 1 到 4 个非零数字字符或 1 到 4 个非零数字字符和 1 个字母组成的字符串的正则表达式

javascript - 实时计算?

json - 在 Go 中使用缩进格式化 JSON

Javascript:根据键的值有条件地过滤 JSON?

javascript - 发送表单数据时显示ajax不正确的结果

javascript - jQueryUI 与 jQuery 3 兼容吗?迁移插件显示弃用

javascript - `if (Date >= Date1 && <= Date2 ){do this}` 不工作

javascript - jQuery:全局变量命名空间问题

javascript - 使用 JSON 从 JavaScript/jQuery 中的 PHP 获取数组,然后对其进行操作?

android - 在 Android 应用资源中使用 JSON 文件