Ios JSON请求正文-目标c格式
我有一个用obj-c编写的iOS应用程序,它接受输入到搜索框中的查询,并在ElasticSearchAPI上执行多匹配字段查询。有人可以帮助设置JSON请求正文的格式吗?这是我想要格式化的get查询主体的一个示例:Ios JSON请求正文-目标c格式,ios,objective-c,json,
elasticsearch,Ios,Objective C,Json,
elasticsearch,我有一个用obj-c编写的iOS应用程序,它接受输入到搜索框中的查询,并在ElasticSearchAPI上执行多匹配字段查询。有人可以帮助设置JSON请求正文的格式吗?这是我想要格式化的get查询主体的一个示例: GET /test/data/_search { "min_score": 1.6,<br/> "query": { <br/> "multi_match": {<br/> "query": "smit
GET /test/data/_search
{
"min_score": 1.6,<br/>
"query": { <br/>
"multi_match": {<br/>
"query": "smith",<br/>
"fields": ["name", "surname"]<br/>
}<br/>
},<br/>
"_source": ["name", "surname", "address"]<br/>
}<br/>
其中,[self.searchTextField text]是从搜索字段中获取的值
但当我从ElasticSearch返回时,当我查询:
reason=“意外字符(','(代码44)):应为冒号
要在[Source:
正如测试一样,使用查询只匹配一个字段效果很好,所以这看起来只是一个格式问题
抱歉,忘记添加了,这是我用JSON正文设置发布到的URL的地方:
[self postDataToUrl:@"http://localhost:9200/test/data/_search?pretty=true" withJson:jsonRequest];
然后,这将传递给:
-(void)postDataToUrl:(NSString*)urlString withJson:(NSString*)jsonString
{
NSData* responseData = nil;
NSURL *url=[NSURL URLWithString:[urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
responseData = [NSMutableData data] ;
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[data length]] forHTTPHeaderField:@"Content-Length"];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"the final output is:%@",responseString);
NSError *lerror = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&lerror];
if (error != nil) {
NSLog(@"Error parsing JSON.");
}
else {
当我通过常规比赛身体时,这一切都很好,例如:
NSString *jsonRequest = [NSString stringWithFormat:@"{\"min_score\":\"1.6\",\"query\":{\"match\":{\"name\":\"%@\"}},\"_source\":[\"name\",\"surname\",\"address\"]}",[self.searchTextField text]];
您可能需要检查搜索结果的内容。看起来您有一个回车符
\n
,它会打断coma之前的字符串。您可以像这样删除回车符:
NSString *newString = [[self.searchTextField text] stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSString *jsonRequest = [NSString stringWithFormat:@"{\"min_score\":\"1.6\",\"query\":{\"match\":{\"name\":\"%@\"}},\"_source\":[\"name\",\"surname\",\"address\"]}", newString];
您可能需要检查搜索结果的内容。看起来您有一个回车符
\n
,它会打断coma之前的字符串。您可以像这样删除回车符:
NSString *newString = [[self.searchTextField text] stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSString *jsonRequest = [NSString stringWithFormat:@"{\"min_score\":\"1.6\",\"query\":{\"match\":{\"name\":\"%@\"}},\"_source\":[\"name\",\"surname\",\"address\"]}", newString];
检查@cybergen的帖子,JSON正文设置不正确,Elastic需要以下格式的JSON查询:
{
"query": {
"multi_match" : {
"query": "smith",
"fields": [ "name", "surname" ]
}
}
}
我格式化了JSON请求字符串以匹配代码中的字符串。检查@cybergen的帖子,JSON正文设置不正确,Elastic希望JSON查询采用以下格式:
{
"query": {
"multi_match" : {
"query": "smith",
"fields": [ "name", "surname" ]
}
}
}
我在代码中格式化了JSON请求字符串以与之匹配。这应该是查询字符串吗?您的JSON无效。在“multi_match”中没有“name”键的值"字典。你不应该在代码中手动创建json字符串,创建一个字典并使用
NSJSONSerialization
将其转换为有效的json字符串。可能重复@MikeD我已经添加了更多信息,希望能让它更清楚。我认为先将jsonRequest转换为字典,然后更新所需的密钥l让您创建一个jsonString,然后通过格式化字符串来更新该值,这样就不太容易出错。这应该是一个查询字符串吗?您的json无效。“multi_匹配”中的“name”键没有值字典。你不应该在代码中手动创建json字符串,创建一个字典并使用NSJSONSerialization
将其转换为有效的json字符串。可能重复@MikeD我已经添加了更多信息,希望能让它更清楚。我认为先将jsonRequest转换为字典,然后更新所需的密钥如果让您创建一个jsonString,然后通过格式化字符串来更新该值,那么就不那么容易出错