IOS本机传递JSON参数中的Worklight适配器调用

时间:2021-12-03 22:10:10

When using JSON.parse(parameter-value) in JavaScript, the adapter invocation is working correctly, however when doing similarly in a native iOS app, it is failing with the following error.

在JavaScript中使用JSON.parse(parameter-value)时,适配器调用正常工作,但是当在本机iOS应用程序中执行类似操作时,它会因以下错误而失败。

Javascript Adapter Call:

Javascript适配器调用:

var invocationData = {
    adapter : 'TEST_ADAP',
    procedure : 'PROC1',
    parameters : [JSON.parse(A)],
};

Native Call:

json= // some json value will be come
 MyConnect *connectListener = [[MyConnect alloc] initWithController:self];
    [[WLClient sharedInstance] wlConnectWithDelegate:connectListener];
    WLProcedureInvocationData *myInvocationData = [[WLProcedureInvocationData alloc] initWithAdapterName:@"TEST" procedureName:@"test"];

    myInvocationData.parameters = [NSArray arrayWithObjects:json, nil];

    for (NSString *str in myInvocationData.parameters) {
        NSLog(@"values of account test %@",str);

    }
    PasswardPage *invokeListener = [[PasswardPage alloc] initWithController:self];
    [[WLClient sharedInstance] invokeProcedure:myInvocationData withDelegate:invokeListener];

2 个解决方案

#1


Your line

myInvocationData.parameters = [NSArray arrayWithObjects:json, nil];

is almost right.

几乎是对的。

The parameters property should be an NSArray (as you did) but the array must be made of string values - NOT a JSON object.

parameters属性应该是NSArray(就像你做的那样)但是数组必须由字符串值组成 - 不是JSON对象。

myInvocationData.parameters = [NSArray arrayWithObjects:@"myValue1", @"myValue2", @"myValue3", nil];

If the data you received is not in this format, you need to first convert it to this format. This is out of the scope of this question.

如果您收到的数据不是这种格式,则需要先将其转换为此格式。这超出了这个问题的范围。

If you are not sure how to convert your existing format into a valid NSArray, please open a new question (tagged with Objective-C, not worklight).

如果您不确定如何将现有格式转换为有效的NSArray,请打开一个新问题(标记为Objective-C,而不是worklight)。

#2


We can pass JSON as NSString in iOS Native code to invoke adapter Example

我们可以在iOS Native代码中将JSON作为NSString传递给调用适配器示例

//Created Dictionary 
NSMutablec *dict = [[NSMutableDictionary alloc]init];
[dict setObject:@"xyz" forKey:@"Name"];
[dict setObject:@"iOS" forKey:@"Platform"];

//Convert it to JSON Data
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                   options:nil
                                                     error:&error];
//JSON Data To NSString

NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

WLProcedureInvocationData * invocationData = [[WLProcedureInvocationData alloc] initWithAdapterName:@"XYZAdapter" procedureName:@"FunctionXYZ"];

//Passing jsonString (NSString Created out of JSON Data) as array to set Parameters.

[invocationData setParameters:[NSArray arrayWithObject:jsonString]];

[[WLClient sharedInstance] invokeProcedure:invocationData withDelegate:self];

#1


Your line

myInvocationData.parameters = [NSArray arrayWithObjects:json, nil];

is almost right.

几乎是对的。

The parameters property should be an NSArray (as you did) but the array must be made of string values - NOT a JSON object.

parameters属性应该是NSArray(就像你做的那样)但是数组必须由字符串值组成 - 不是JSON对象。

myInvocationData.parameters = [NSArray arrayWithObjects:@"myValue1", @"myValue2", @"myValue3", nil];

If the data you received is not in this format, you need to first convert it to this format. This is out of the scope of this question.

如果您收到的数据不是这种格式,则需要先将其转换为此格式。这超出了这个问题的范围。

If you are not sure how to convert your existing format into a valid NSArray, please open a new question (tagged with Objective-C, not worklight).

如果您不确定如何将现有格式转换为有效的NSArray,请打开一个新问题(标记为Objective-C,而不是worklight)。

#2


We can pass JSON as NSString in iOS Native code to invoke adapter Example

我们可以在iOS Native代码中将JSON作为NSString传递给调用适配器示例

//Created Dictionary 
NSMutablec *dict = [[NSMutableDictionary alloc]init];
[dict setObject:@"xyz" forKey:@"Name"];
[dict setObject:@"iOS" forKey:@"Platform"];

//Convert it to JSON Data
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                   options:nil
                                                     error:&error];
//JSON Data To NSString

NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

WLProcedureInvocationData * invocationData = [[WLProcedureInvocationData alloc] initWithAdapterName:@"XYZAdapter" procedureName:@"FunctionXYZ"];

//Passing jsonString (NSString Created out of JSON Data) as array to set Parameters.

[invocationData setParameters:[NSArray arrayWithObject:jsonString]];

[[WLClient sharedInstance] invokeProcedure:invocationData withDelegate:self];