如何将JSONValue与NSMutableArray一起使用 [英] How to use JSONValue with NSMutableArray

查看:118
本文介绍了如何将JSONValue与NSMutableArray一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,它不起作用

here is my code and It doesn't work

NSError *theError = nil;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.bbblllaaahhh.com"]];
NSURLResponse *theResponse =[[NSURLResponse alloc]init];    
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];      
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

self.city = [[NSMutableArray arrayWithArray:[string componentsSeparatedByString:@"\""]] JSONValue];

这里是JSON文本

    [
{
"kanji_name":"\u30ac\u30fc\u30c7\u30f3\u30d5\u30a3\u30fc\u30eb\u30ba\u3000\u3068\u306d\u308a\u516c\u5712BigBell"
}
]

它以self.city行报告,应该是什么我这么做了?

It reports in self.city line , What should I do ??

是啊!!我完全修复了这里是我的修复代码

Yeah!! I completely fix it Here is My fix code

NSError *theError = nil;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.blahblah.com"]];
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];   
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [string JSONValue];
NSArray *jsonArray = [NSArray arrayWithArray:(NSArray *)[jsonDict valueForKey:@"kanji_name"]];
NSMutableString *text = [[NSMutableString alloc] init];
[text appendFormat:@"%@",[jsonArray objectAtIndex:0]];
self.city = [NSMutableArray arrayWithObject:text];


推荐答案

需要几个问题来帮助确定回答这个问题。

A couple of questions will be needed to help determine the answer to this problem.

首先,你可以放置

NSLog(@"%@",data);

第一行后

after the first line and

NSLog(@"%@", string);

在第二行之后告诉我们它向控制台报告的值是多少?这将有助于确定问题是否

after the second line and tell us what value it reports to the console? This will help determine if the problem is

1)服务器永远不会返回任何数据或返回错误的数据和
2)如果数据正确转动成一个字符串。如果其中任何一个操作失败,可能会导致第3行出错。

1) That the server never returns any data or returns the wrong data and 2) If the data is correctly turned into a string. If either of these actions fails, it may cause an error in line 3.

接下来,您能报告第三行给出的错误吗?有许多可能的问题。事实上,字符串可能不是正确的JSON代码,而JSON解析器正在崩溃。

Next, could you report what error the third line is giving? There are many possible problems. It could be that the string is not, in fact, proper JSON code and the JSON parser is crashing.

第3行看起来有一个明显的问题。首先,您将基于\字符拆分字符串,这在这种情况下似乎是一件不寻常的事情。但无论如何,操作的顺序如下:

Line 3 looks like it has one obvious problem. First, you are splitting a string based on the "\" character, which seems like an unusual thing to do in this situation. But anyway, the order of operations will look like this:

@a \\\

将转为

[a,b]

然后JSON解析器将尝试解析[a,b],这肯定会导致错误。

then the JSON parser will try to parse ["a", "b"], which will certainly cause an error.

至少,你会想做类似的事情,

At the very least, you will want to do something like,

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];      
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSArray or NSDictionary *parserResults = [string JSONValue];

// This will depend on what the actual JSON string returned from the server
// With an array, maybe something like
NSString *stringWithBackslash = [NSArray objectAtIndex:0];

// With a dictionary, maybe something like
NSString *stringWithBackslash = [NSDictionary objectForKey:@"backslashString"];

self.city = [NSMutableArray arrayWithArray:[stringWithBackslash componentsSeparatedByString:@"\""]];

self.city是一个NSMutableArray吗?变量名称听起来应该是一个字符串。在这种情况下,你实际上想做一些像

Is self.city an NSMutableArray? The variable name makes it sound like it should be a string. In that case, you would actually want to do something like

NSMutableArray components = [NSMutableArray arrayWithArray:[stringWithBackslash componentsSeparatedByString:@"\""]];

// if the city is the first element of the array
self.city = [components objectAtIndex:0];

您还需要检查以确保组件(例如)具有多个元素,因为也可能导致错误,例如,如果服务器返回错误或没有互联网连接。

You will also want to check to make sure that components, for example, has more than element because that also might cause an error, if, for example, the server returned an error or there were no internet connection.

这篇关于如何将JSONValue与NSMutableArray一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆