Consuming an API, Using GetJSON


#1

Hello NSBasic Users! I am new to this development environment and am trying to build a simple app that shows Outages for a telecom company. They have set me up with an API that pulls the following JSON file or document tree. First question is, in the GetJSON sample app it shows the JSON file in the example. I am assuming I can use the API as the URL instead of the “datafile.json” that is given in the example. Is this true for the URL parm? 2nd Question, in the output below, I have 2 “outages” that are noted by ID=1 and ID=2. Does anyone have any code examples for pulling in just the TITLE data elements for the two?

John.McReynolds 2017-04-25T00:00:00 2017-04-30T00:00:00 1 true false 0 Stanton DSL Outage Jerry.Creek 2017-04-25T00:00:00 2017-04-28T00:00:00 2 false false 0 Sullivan Cable Outage

#2
  1. Yes, the first parameter can be a URL.
    http://wiki.nsbasic.com/GetJSON

  2. Unless they have special arrangements in the UI, you are probably going to get the entire JSON structure. Use console.log(data) to see how it looks.

If everything is easy, the TITLE will be in

console.log(data.TITLE)

George Henne
NS BASIC Corporation
http://www.nsbasic.com


#3

Ok, the JSON from the URL is coming back. It is a valid JSON object. I set the Textbox to just tell me if the JSON is valid to see if it gets to the callback function. It does not. In the console, any of the following gives me the following: (any further insights are appreciated).

console.log(data.TITLE)
console.log(data[0])
console.log(data[0].Title)

“data is not defined
at eval (eval at (:1:1), :1:13)
at :1:1”

Function Button1_onclick()
Dim id, url, title
id = “q=” & 1
’title = “q=” & encodeURIComponent(Textarea1.value)
url = "http://fodreporting/outage/api/outagedata?callback=?"
GetJSON(url, myFunction)

End Function

Function myFunction(data)
Textarea1.value = “Callback success. JSON is valid.”
'Textarea1.value = data.title
End Function


#4

Additionally, I just realized I needed to identify the callback function as I use it in the URL parm:

url = “http://fodreporting/outage/datafile.json?callback=myFunction

But I am receiving Access Control Origin errors. When I remove the callback in the URL, I get and see the JSON but the callback function specified by the getJSON does not fire. Danged if I do, and danged if I don’t.

No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

I do have the dot net application opened to Access Control from every domain also. Still digging.


#5

Ok, I am answering my own support inquiries. You guys know what you are doing. LOL

Publishing to a webserver instead of running locally goes A LONG WAY!


#6

Good work! This shows you’re really beginning to catch to this stuff.

Thanks for the good news.

George Henne
NS BASIC Corporation
http://www.nsbasic.com


#7

George, thanks much. Integrating with the API now with the Collapsible JQUERY Mobile widget. Trying to stick with the NSBASIC language… but may go to JS.


#8

You can actually mix the two in the same app:

http://wiki.nsbasic.com/Javascript...End_Javascript

Also, if you select some BASIC in the Code Window and right click, you can see how it translates to JavaScript.


#9

Very nice feature. In the documentation for the Jquery Collapsible, the NSBasic cites an example:


#10

Example
See Collapsible sample in Samples/2. Controls/jQuery Mobile.

But the example offers no code help, just the Collapsible widget in black and white with no code on how to addChild(). Is there an example of this I am missing?


#11

You can add controls to a Collapsible at Design Time or Run Time.

At Design Time, drag to control on top of the Collapsible and it will be added.

At Run Time, do this:

Collapsible2.addChild(Button1)


#12

Awesome. This is working great! We are very excited!


#13

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.