0

I've been using the following function for some time with great success, but just recently it stopped working. I've not changed anything; same browser, server, same jquery library, everything's the same so I'm lost on what's wrong. The following function takes a long string and converts it to an object:

$(function()
{   
    var _sid    = getUrlVar('AICC_SID');
    var _url    = getUrlVar('AICC_URL');
    $.get(_url,{command:"GetParam",version:"2.2",session_id:_sid},function(response)
    {
        var _obj = toJSON(response);
        console.log(_obj);
    });
});

function toJSON(str) 
{
    var obj = {};
    str.replace(/([^=]+)=(.*)\n/g, function (_, name, value)
    {
        console.log('name : ' + name);
        console.log('value : ' + value);
        obj[name] = $.trim(value);
    });
    return obj;
}

Here's the string that get's converted:

ERROR=0
ERROR_TEXT=Successful
VERSION=2.2
AICC_DATA=[CORE]
STUDENT_ID=0425655
STUDENT_NAME=Doe, John B
SCORE=
TIME=00:00:00
CREDIT=C
LESSON_LOCATION=1_5
LESSON_STATUS=NULL
[Core_Lesson]
[Objectives_Status]

The problem is the .replace() appears to not be running. The toJSON() function converts the string to an object, but it always returns and empty object. Hopefully that clears up what's wrong.

Mike
  • 1,760
  • 5
  • 21
  • 40
  • What errors are you seeing? – Mark Walters Nov 08 '13 at 16:01
  • 2
    [Attach a debugger and test it](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) – Liam Nov 08 '13 at 16:02
  • 2
    The last two lines don't match your regex, nor do they meet your requirements for generating key/value pairs on your object – CodingIntrigue Nov 08 '13 at 16:02
  • No errors are being thrown, the .replace() is not even running. – Mike Nov 08 '13 at 16:04
  • 1
    I didn't downvote, but I imagine it's because you didn't describe what was actually wrong. You just said it wasn't working. Questions must: "Include *attempted solutions*, *why they didn't work*, and *the expected results*" – CodingIntrigue Nov 08 '13 at 16:08
  • Are you sure the `$.get` is executing the success handler? Per http://api.jquery.com/jQuery.get/: "If a request with jQuery.get() returns an error code, it will fail silently unless the script has also called the global .ajaxError() method." – pete Nov 08 '13 at 16:12
  • By "`.replace()` appears not to be running" you mean that you don't see the console.log messages in the console either? – JJJ Nov 08 '13 at 16:16
  • @Juhana that is correct. The `console.log()` in the `$.get()` works fine, but the `console.log()` in the replace never fires. – Mike Nov 08 '13 at 16:19
  • ...that means `toJSON()` isn't being called, not that the regex isn't working. – JJJ Nov 08 '13 at 16:28
  • I thought `toJSON()` wasn't firing, but I put an `alert()` to see and the `alert()` fired. So I'm positive `toJSON()` is firing. – Mike Nov 08 '13 at 16:29

1 Answers1

0

Try removing the \n from your RegEx.

So it looks like this:

str.replace(/([^=]+)=(.*)/g, ...

I'm guessing it's a problem related to line breaks.

UPDATE

Try updating toJSON() with this:

function toJSON(str) {
        var obj = {};
        str.replace(/([^=]+)=(.*)/g, function (_, name, value) {
            name = name.replace(/\n/g, '');

            console.log('name : ' + name);
            console.log('value : ' + value);
            obj[name] = $.trim(value);
        });
        return obj;
    }
zgood
  • 12,181
  • 2
  • 25
  • 26
  • your suggestion is working, but the string I'm converting appears to have `/n` after each line, so in the `console.log()` it's showing a new line. – Mike Nov 08 '13 at 16:34
  • I have updated the answer. Let me know if you have any success. – zgood Nov 08 '13 at 16:46
  • Just tried it and it doesn't appear to do anything to the`/n`. – Mike Nov 08 '13 at 18:25