-2

How can we change JSON String to a Javascript Object using JSON.parse for an API URL?

We need Full Calendar to GET events from our API URL. Our API has a JSON String, Full Calendar needs a Javascript Object, we are trying to convert it to a Javascript Object using JSON.parse How can we edit var javascriptObj = JSON.parse(jSonString); with the following code to work with a URL?

Thanks so much in advance!

OUR JAVASCRIPT:

$(document).ready(function() {

$('#calendar').fullCalendar({
    //theme: true,
    header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay'
    },
    defaultDate: moment().format("YYYY-MM-DD"),
    editable: true,
    events: {
        url: 'URL WE WANT TO KEEP PRIVATE',
        type: 'GET',
       contentType: "application/json; charset=utf-8",
      dataType: "json",
      contentType: 'application/json; charset=utf-8',
        success: function(resultData) {
            //here is your json.
              // process it

        },
        error : function(jqXHR, textStatus, errorThrown) {
        },
    }

});

});
IKid
  • 43
  • 7
  • Is `resultData` already JSON and you just need to do something with it, or does it actually need to be parsed? You could give an example of the data returned and what you'd like to do with it. – Goose Apr 13 '16 at 21:26
  • You don't need to know how to parse JSON; `JSON.parse()` exists. – Pointy Apr 13 '16 at 21:26
  • Thanks for the response! @Goose The JSON on the API URL needs to be parsed. – IKid Apr 13 '16 at 21:27
  • @Pointy What would I edit to the Javascript for it to JSON.parse() the URL? Thanks! – IKid Apr 13 '16 at 21:28
  • You may not need to do anything at all; it may be parsed already by the time your "success" callback is invoked. Add `console.log(typeof resultData)` to the top of the success function. If it logs `[object Object]` then it's already parsed. – Pointy Apr 13 '16 at 21:29
  • 1
    *"parse the URL"* makes no sense. can you rephrase? – Kevin B Apr 13 '16 at 21:30
  • @Pointy I'm afraid it didn't work. Any other ideas? Thanks again! – IKid Apr 13 '16 at 21:33
  • *What* didn't work? And what exactly does "didn't work" mean? What did you try? What happened? Errors? Weird behavior? Anything? People cannot read your mind. – Pointy Apr 13 '16 at 21:36
  • @Pointy Sorry, it didn't get any events, nothing displayed. That's what we're trying to fix. The JSON on our API is not in JS, so Full Calendar is not reading it. – IKid Apr 13 '16 at 21:38
  • *The JSON on our API is not in JS* - what does that even mean? Have you checked the developer console to see if any HTTP requests are happening (or even for errors)? – Pointy Apr 13 '16 at 21:41
  • @Pointy No errors, the code works to get events from other APIs formatted correctly. I meant a Javascript object, see here: http://stackoverflow.com/questions/8294088/javascript-object-vs-json – IKid Apr 13 '16 at 21:44
  • @Pointy I edited the question, if it helps at all – IKid Apr 13 '16 at 21:49
  • "JSON.parse for an API URL" - what does this mean?! – evolutionxbox Apr 13 '16 at 21:54
  • Are you asking how to get the events from a remote api rather than having to have them in the code? because that's not at all what your question seems to be asking, but is the only thing i can think of from the mangled mess that it is plus comments. http://fullcalendar.io/docs/event_data/events_json_feed/ – Kevin B Apr 13 '16 at 21:54
  • @KevinB Sorry to all, I'm having trouble explaining it. Here is a link to my original question, the answer explains quite well. http://stackoverflow.com/questions/36577800/json-wont-get-events-in-full-calendar-from-url/36578907?noredirect=1#comment60790160_36578907 My issue is that our API is a JSON String, but Full Calendar can only get events from Javascript Objects, we need to do JSON.parse with our API Link, to turn the JSON String in our API link into Javascript Object. – IKid Apr 13 '16 at 22:23
  • @evolutionxbox Sorry to all, it's been edited. Let me know if it's any better – IKid Apr 13 '16 at 22:25
  • @IKid *"Full Calendar can only get events from Javascript Objects"* that is absolutely false. see the docs link i provided. – Kevin B Apr 13 '16 at 22:26
  • Why are there two questions about the same problem? – evolutionxbox Apr 13 '16 at 22:28
  • @Someone answered our question yesterday (the link provided above), saying that's what was wrong with our API content. Do you have any idea why it wouldn't work formatted the way it is. Thanks again. API CONTENT: [{"title":"test","start":"2016-04-13","end":"2016-04-13","id":"75bac5c63092f816"}] – IKid Apr 13 '16 at 22:28
  • @evolutionxbox The original question only solved part of the question. I didn't understand the JSON.parse wouldn't work with a URL, the way he answered. – IKid Apr 13 '16 at 22:29
  • Why is the API result IN the url? Surely it won't be a valid URL... – evolutionxbox Apr 13 '16 at 22:30
  • Sounds like you need a middleman service on your server that will take a 3rd party json string and convert it to the format you need. – Kevin B Apr 13 '16 at 22:31
  • @evolutionxbox No, that's the contents of the API, not the URL. I just wanted to show the format – IKid Apr 13 '16 at 22:32
  • @KevinB Okay, thanks for tip! I'm suprised Deployd doesn't do this. – IKid Apr 13 '16 at 22:32

2 Answers2

-1

There's probably a jQuery way to do this, but here's a vanilla javascript approach.

    success: function(resultData) {
        var resultObject
        //here is your json.
        try {
            resultObject = JSON.parse(resultData);
        } catch(e) {
            console.error("Error parsing JSON:", e);
            throw(e);
        }

        // ... Do something with resultObject
          // process it

    },
evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
Ray Wadkins
  • 876
  • 1
  • 7
  • 16
-3

if you need to change the json to javascript object you can to use "Object.create" function, this method creates a new object. check this: enter link description here

  • it's hard to see how this is an answer to the (terrible) question – Rob G Apr 13 '16 at 21:49
  • if you need to change the json to javascript object you can to use "Object.create" function, this method creates a new object. check this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create – Evinton Antonio Cordoba Mosque Apr 14 '16 at 01:08