0

My javascript function needs this type of array:

var data = [{
                                    "sale": "202",
                                    "year": "2000"
                                    }, {
                                    "sale": "215",
                                    "year": "2002"
                                    }, {
                                    "sale": "179",
                                    "year": "2004"
                                    }, {
                                    "sale": "199",
                                    "year": "2006"
                                    }, {
                                    "sale": "134",
                                    "year": "2008"
                                    }, {
                                    "sale": "176",
                                    "year": "2010"
                                    }];

I want to pass this array as an argument from [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"js_func(%@, %@)", y1Arr, y2Arr]];

I am passing array of dictionary with this format:

({
    x = "663.78";
    y1 = "-0.000";
},
{
    x = "663.88";
    y1 = "0.005";
},
{
    x = "663.98";
    y1 = "0.019";
},
{
    x = "664.08";
    y1 = "-0.001";
}
)

But its not working...

2
  • There is no dictionary type in JavaScript. Use [] instead of () in your last snippet to make it an array. Oh and what is "not working"? Commented Feb 27, 2015 at 8:28
  • ({ x = "663.78"; y1 = "-0.000"; }, { x = "663.88"; y1 = "0.005"; }, { x = "663.98"; y1 = "0.019"; }, { x = "664.08"; y1 = "-0.001"; } ), this is the log from debugger. My JS function is not getting called. I tried putting and alert there, but no luck. Commented Feb 27, 2015 at 8:38

1 Answer 1

1

You shouldn't pass directly your arrays in the stringWithFormat, you should first convert them into json objects and then pass those json objects (as string representation) to the stringWithFormat

NSData *data1 = [NSJSONSerialization dataWithJSONObject:y1Arr options:0 error:NULL];
NSString *y1ArrStr = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];

NSData *data2 = [NSJSONSerialization dataWithJSONObject:y2Arr options:0 error:NULL];
NSString *y2ArrStr = [[NSString alloc] initWithData:data2 encoding:NSUTF8StringEncoding];

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"js_func(%@, %@)", y1ArrStr, y2ArrStr]];
Sign up to request clarification or add additional context in comments.

2 Comments

OK, function is working now.. but when I try ti alert(arrayData), it displays weird values like: [object Object], [object Object]
For javascript things like { x = "663.78"; y1 = "-0.000"; } are objects, and it doesn't know how to render them inside an alert, try alert(arrayData[0].x)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.