繁体   English   中英

Objective-C将json解析为对象列表

[英]Objective-C parse json to list of object

我需要将json字符串解析为NSMutableArray ...我这样做如下:

    JsonString = "{
   "list":[
      {
         "IDI":{
            "IDI_ID":1
         },
         "PAR_VPARAM":"param1",
         "PAR_VALUE":"value1"
      },
      {
         "IDI":{
            "IDI_ID":2
         },
         "PAR_VPARAM":"param2",
         "PAR_VALUE":"value2"
      },
      {
         "IDI":{
            "IDI_ID":3
         },
         "PAR_VPARAM":"param3",
         "PAR_VVALUE":"value3"
      }
   ]
}";


NSData *data = [JsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&err];
NSMutableArray *resultArray = [json objectForKeyedSubscript:@"list"];

我有一个名为PARAMETERS的对象,它具有与JSON单个项目“ list”相同的结构。 当我解析它时,它起作用了,问题出在json中每个项目内部的对象中:“ IDI”,始终使用null值进行解析。

for(NSObject *obj in resultArray){
     Parameters *paritem = (Parameters *)obj;
     int test = paritem.IDI.IDI_ID; //Error here!
}

我该怎么做?

NSJSONSerialization不会将您的JSON数据映射到您的自定义类。 它将提供NSStringNSNumberNSDictionaryNSArray对象(如果您指定了正确的NSJSONReadingOptions ,则将提供其可变对象)。

如果要将这些数据映射到Parameters类,则必须自己提供该逻辑。 您不能简单地转换NSDictionary

for(NSObject *obj in resultArray){
    Parameters *paritem = [[Parameters alloc] init];

    paritem.PAR_VPARAM = obj[@"PAR_VPARAM"];
    paritem.PAR_VALUE = obj[@"PAR_VALUE"];

    // To capture the IDI property, you will either have to
    // define a new IDI class with a property named "IDI_ID",
    // live with NSDictionary, or add an "IDI_ID" property
    // to your Parameters class.

    // In this example, I left the value as a dictionary.
    paritem.IDI = obj[@"IDI"];

    // Here's how you would get the IDI_ID:
    NSNumber *IDI_ID = paritem.IDI[@"IDI_ID"];
}

有了这些,下面是一些未经请求的样式提示:

  • 对于Obj-C中的变量和属性,lowerCamelCase是常规的。 代替paritem.PAR_VPARAM ,使用parItem.parVParam (注意parItem.parVParam的大写parItem I )。
  • 您的类名称应具有两个或三个字母的“命名空间”(非常类似于NSStringUIViewCGPoint )。 如果您不能用几个字母代表这个特定项目,请使用公司名称的缩写。 如果所有其他方法均失败,请使用您的姓名缩写。
  • 您的参数名称非常模糊,并且有些多余。 是否真的需要每个属性都以PAR_作为前缀? 您是否真的需要将IDI_ID嵌套在Parameters对象的IDI属性中? 您可以通过更加简洁来使您的代码更具可读性。

如果您采纳此建议,则代码可能如下所示(我对源数据进行了一些假设):

for(NSObject *obj in resultArray){
    APParameters *parItem = [[APParameters alloc] init];

    parItem.parameterName = obj[@"PAR_VPARAM"];
    parItem.parameterValue = obj[@"PAR_VALUE"];

    // To capture the IDI property, you will either have to
    // define a new IDI class with a property named "IDI_ID",
    // live with NSDictionary, or add a property to your
    // Parameters class which holds the IDI_ID value directly.

    // In this example, I grabbed the IDI_ID value directly.
    parItem.itemID = obj[@"IDI"][@"IDI_ID"];
}

首先,如果您不打算在数组中添加或删除新对象,建议您将JSON数据而不是NSMutableArray转换为NSArray

关于"IDI"索引,由于它们是字典,因此不会像其他索引一样被解析为字符串。 您还应该手动创建Parameters对象,而不是直接强制转换。

一个例子:

// In Parameters.h
@property (nonatomic, strong) NSString *PAR_VPARAM;
@property (nonatomic, strong) NSDictionary *IDI;

然后在解析JSON之后,

for (NSDictionary *obj in resultArray){
     Parameters *paritem = [[Parameters alloc] init];
     paritem.PAR_VPARAM = [obj valueForKey:@"PAR_VPARAM"];
     paritem.IDI = [obj valueForKey:@"IDI"];
     int test = (int)[paritem.IDI valueForKey:@"IDI_ID"];
}

这应该工作良好,并且您可以为其他JSON索引创建新属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM