-5

Can someone show me how the syntax work to get the data from the JSON of URL by javascript? An example: JSON in the url

({
  "test": 60,
  "homework": 15,
  "quiz": 0,
  "class_participation": 10,
  "final_exam": 15,
  "success": true
})

The number represents the weight of the categories, after I get the data out from the URL I want it to automatically fill out all the input box with respectively values.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

0

First of all, you need to know how to fetch data from URL, and how to store data into URL.

For storing data into URL, you should normalize it that it does not change and does not make you an ugly or unsafe URL.

Let's say your data is in data variable which is a JavaScript object.

var data = {
  "test": 60,
  "homework": 15,
  "quiz": 0,
  "class_participation": 10,
  "final_exam": 15,
  "success": true
}

var URLData = ncodeURIComponent(JSON.stringify(data));

// Pass URLData into URL

Now it's time to read, you should use JSON.parse(your-object) to read data you read from URL.

Now you need to access items in your JavaScript object.

For the data variable, this is how we get items:

data.test; // --> 60
data.homework;  // --> 15
data.success  // --> true
Mohammad Kermani
  • 5,188
  • 7
  • 37
  • 61
  • I am confusing on how can I fetch data from URL. I tried to look multiples solution but I still lost. Assume, I have URL:http://cs1371.gatech.edu/getClassInfo/?class=CS1371. How can I get the data and store them in the variable? Can you give me some more hint? – Thinh Nguyen Nov 26 '17 at 19:40