Iphone 是否将JSON数据检索为NSString而不是整数?

Iphone 是否将JSON数据检索为NSString而不是整数?,iphone,xcode,json,nsstring,integer,Iphone,Xcode,Json,Nsstring,Integer,下面的代码仅在我的JSON数据是一系列整数时有效,例如[11,12,13]。我怎样才能让它检索一条消息/短语呢 - (IBAction)checkmessages:(id)sender { responseData = [[NSMutableData data] retain]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"file:///Us

下面的代码仅在我的JSON数据是一系列整数时有效,例如[11,12,13]。我怎样才能让它检索一条消息/短语呢

- (IBAction)checkmessages:(id)sender
{
        responseData = [[NSMutableData data] retain];       
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"file:///Users/Alex/Desktop/Test.json"]];
        [[NSURLConnection alloc] initWithRequest:request delegate:self];
    }
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {      
    [connection release];

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [responseData release];

    NSError *error;
    SBJSON *json = [[SBJSON new] autorelease];
    NSArray *luckyNumbers = [json objectWithString:responseString error:&error];
    [responseString release];   

    if (luckyNumbers == nil)
        label.text = [NSString stringWithFormat:@"JSON parsing failed: %@", [error localizedDescription]];
    else {      
        NSMutableString *text = [NSMutableString stringWithString:@"Latest Message:\n"];

        for (int i = 0; i < [luckyNumbers count]; i++) 
            [text appendFormat:@"%@\n", [luckyNumbers objectAtIndex:i]];

        label.text =  text;
    }
}
-(iAction)检查消息:(id)发件人
{
responseData=[[NSMutableData]retain];
NSURLRequest*request=[NSURLRequest requestWithURL:[NSURL URLWithString:@]file:///Users/Alex/Desktop/Test.json"]];
[[NSURLConnection alloc]initWithRequest:请求委托:self];
}
}
-(void)ConnectionIDFinishLoading:(NSURLConnection*)连接{
[连接释放];
NSString*responseString=[[NSString alloc]initWithData:responseData编码:NSUTF8StringEncoding];
[回应数据发布];
n错误*错误;
SBJSON*json=[[SBJSON新建]自动释放];
NSArray*luckyNumbers=[json objectWithString:responseString错误:&error];
[责任释放];
如果(幸运数字==nil)
label.text=[NSString stringWithFormat:@“JSON解析失败:%@,[error localizedDescription]];
否则{
NSMutableString*text=[NSMutableString stringWithString:@“最新消息:\n”];
对于(int i=0;i<[幸运数字计数];i++)
[文本附件格式:@“%@\n”,[luckyNumbers对象索引:i]];
label.text=文本;
}
}
编辑:
当我的JSON文件看起来像:[10,11,12]时,它工作正常,但如果我将其更改为:[Message 1,Message 2],我会收到错误:“JSON解析失败:解析数组时的预期值”

“字符串”必须括在引号中(
)。

您的JSON看起来格式不正确

 [Message 1,Message 2]
应该是

 ["Message 1", "Message 2"]

你有什么问题?如果JSON数据以字符串的形式返回,它怎么会失败呢?当我的JSON文件看起来像:[10,11,12]时,它工作得很好,但是如果我将其更改为:[Message 1,Message 2],我会得到错误:“JSON解析失败:解析数组时的预期值”我认为还缺少一些引号,即:[“Message 1”,“Message 2”]很高兴它这样做了:),我加上它作为答案。