ios - iOS-尝试从JSON获取值(value)

标签 ios objective-c json

我不知道为什么我没有从json中获取值。

{“user_id”:“150”,“user_name”:“示例”,“user_lastname”:“Ex”}

我使用以下代码:

  NSData* data = [string dataUsingEncoding:NSUTF8StringEncoding];

NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

for (NSMutableDictionary *dictionary in array)
{

    if([dictionary isEqual:@"user_id"]){

    self.userId = [dictionary valueForKey:@"user_id"];  
    }

感谢您的帮助。

最佳答案

  NSData* data = [string dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];


    self.userId = [dictionary valueForKey:@"user_id"];  

尝试这个。您的JSON表示已经说它是字典(即{}代表字典)

关于ios - iOS-尝试从JSON获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22930956/

相关文章:

ios - 如何从iOS中的经度和纬度获取ZipCode?

ios - 在 UIKit 中为图像添加阴影

iOS 6+ : UITableViewController embedded into Navigation Controller shows table's first section header under status bar when Navigation Bar is hidden

objective-c - 将参数传递给 Objective C 中的 JSON Web 服务

c# - 如何让 HttpClient Json 序列化程序忽略空值

iOS 贝塞尔曲线路径显示为之字形

ios - 创建自定义选取器 View

ios - 你将如何重新实现 UIControl?它如何适应响应者链?

c# - 将C#对象序列化为json

android - Android 2.1中JSONObject的Base64解码