Problem with string.replace on json parse

You must Login before you can answer or comment on any questions.

I am having a problem parsing some complicated json from a webservice, and I cannot get the .replace method to work.

jsonArray = JSON.parse(d.responseText);
        /*
        var title = jsonArray.suggestions.homecare_options;
        title = title.replace(/{/g, ' ');
        */
        var myString = jsonArray.suggestions.therapies.therapeutic_procedures;
 
        var option = Ti.UI.createLabel
        ({
            text: myString.replace(/({:""})/g,"="),
            top: 10,
            color: 'black'
        });
        view.add(option);
I keep getting an error undefined is not a function

3 Answers

Accepted Answer

HI Andrew, can you put the log message here which says undefined is not a function... there might be some function or variable name.

and

also you can check the type o title and myString on which you are calling replace method... may be they are not string... use typeof() function to check the type

— answered 7 months ago by Ashish Nigam
answer permalink
8 Comments
  • Ashish gives you some good ideas -- adding logging could help you isolate the problem.

    Setting a breakpoint and inspecting the variables can be faster, though, and it's often more flexible. If you try to use logging alone, you have to have a good idea of what the problem is -- with inspection, you can look at a lot of different variables all at once and at arbitrary points in the program's execution. You often find something you weren't expecting.

    — commented 7 months ago by Jason Priebe

  • [ERROR]  Exception in event callback: {
        line = 62;
        message = "'undefined' is not a function (evaluating 'myString.replace(/({:\"\"})/g,\"=\")')";
        name = TyepError;
        sourceId = 190860480;
    }
    is the error message I am getting.

    I believe you may be right that it is not a string, but I can place it into a uiTextLabel and it shows up on the simulator I am just trying to eliminate some excess around the text I want.

    Thank you

    — commented 7 months ago by Andrew Corliss

  • Sometimes variables will be cast from one type to another, so the fact that you can put it into a Label doesn't mean it's a string. You could also put an int variable into a text label, but it won't have a replace() function.

    Have you tried debugging yet?

    — commented 7 months ago by Jason Priebe

  • Show 5 more comments

Hello andrew,

for replace char in perticular string, you can also use RegExp().

see this Link.

Hope it helps you...

I suspect that myString doesn't contain what you think it does. Have you tried debugging this code? You need to set a breakpoint and inspect your variables. You may already be familiar with the debugger, but if not, have a look at this video.

Your Answer

Think you can help? Login to answer this question!