1

So I have a php page that returns a JSON object in the correct format. like:

  [ 
    {
    "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": []
    }
    ]
  }
]

I need to take this JSON object and transfer it to a variable in my script in the form of

var obj = k;

where k is the JSON object returned from this URL. I have imported jQuery and tried the $.getJSON method but with no success. So if you have a URL 'ex.com' with the JSON object how would I save it to a variable exactly as it is. So if I copy the output from the url directly to a variable like:

var obj2 = copiedJSON;

my function works as intended but when I use $.getJSON like:

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

My function returns an error. Does the JSON function change the output that is returned.

df fa
  • 11
  • 3

1 Answers1

1

You didn't post the code that you tried for $.getJSON, so I can't tell you why that is broken, but here is how you would use $.getJSON to achieve your solution:

// 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>

Note that the json variable will only be avaliable in the function you provide to $.getJSON, this is because in GET requests are extecuted asynchronously (e.g. the rest of your code keeps running while the request is bring made), so just make sure that any code that you are going to run that relies on the json is put inside the callback function.

jeffkmeng
  • 851
  • 1
  • 8
  • 30
  • Yeah I updated the question. I tried your method but the function returns an error with this method. While it works if I directly copy the output from the url – df fa Dec 15 '19 at 04:55
  • What error are you getting? – jeffkmeng Dec 15 '19 at 04:59
  • Its returning the JSON object and it looks the same as the one from the url. However it just doesn't work in the same way as when I copy the contents directly to the variable. I logged it in console as well. This is very confusing. Is the a possibility that getJson changes the format in some way – df fa Dec 15 '19 at 05:05
  • You shouldn't need to copy the contents to a variable. Why don't you just use the json variable? You can rename it as needed. If you are copying that variable to another, you may need to clone it. See https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript – jeffkmeng Dec 15 '19 at 05:06
  • Omg that worked I put the entire script within the getJSON function and it worked. I guess it doesn't get copied to a variable with the function. – df fa Dec 15 '19 at 05:15
  • The reason it worked when you put the entire script within the function was because, as I stated above, when your function ran, the JSON variable didn't yet have a value. If my above answer is a good answer to your question, I would appreciate it if you accepted it. – jeffkmeng Dec 15 '19 at 05:18