我正在使用jsonmodel来序列化操作。我发送邮件到服务器并获取json数据。我将数据反序列化为此对象。
#import <Foundation/Foundation.h>
#import "JSONModel.h"
@interface ResultObject : JSONModel
@property (strong, nonatomic) NSObject<Optional> *Data;
@property (strong, nonatomic) NSString *ResultCode;
@property (strong, nonatomic) NSString *ResultMessage;
@property (strong, nonatomic) NSObject<Optional> *Exception;
@end
我可以获得简单的数据。像布尔值或字符串。但是当我尝试将数据转换为我的自定义对象时。数据已损坏。 我正在使用此代码进行投射。
ResultObject *resultObject = [[ResultObject alloc]initWithString:result error:&error];
NSString *returnAnswer = [NSString stringWithFormat:@"%@",resultObject.Data];
LanguagePack *pack =[[LanguagePack alloc]initWithString:returnAnswer usingEncoding:NSASCIIStringEncoding error:&error];
冒号(:)更改为等于(=) 并且逗号(,)在returnAnswer中更改为分号(;),因此“pack”为空。我无法反序列化json数据。
这是我的LanguagePack
@interface LanguagePack : JSONModel
@property(strong,nonatomic) NSArray<LanguageString> *Data;
@end
这是我的LanguageString
@protocol LanguageString;
@interface LanguageString : JSONModel
@property (strong, nonatomic) NSString *DataKey;
@property (strong, nonatomic) NSString *DataValue;
@property (strong, nonatomic) NSString *DataDescription;
@property (strong, nonatomic) NSString *DataLanguage;
@end
我的问题是如何将NSObject(数据)中的json数据反序列化为自定义对象?
编辑注意:当我查看ResultObject.Data时,它有14个对象(应该是)但每个对象都有错误:
expected']'错误:解析表达式时出错1个
像这样。如果我将NSObject<Optional> *Data
更改为NSArray<LanguageString> *Data
则可以正常使用。但我需要像NSObject这样的通用类型。
我的Json
{
"Data":[
{
"DataKey":"AppTemplate.CancelButton.Text",
"DataValue":"Iptal",
"DataDescription":"",
"DataLanguage":"TR"
},
{
"DataKey":"Exception.Code.07",
"DataValue":"SMS dogrulama kodu hatali ya da zaman asimina ugramis.",
"DataDescription":"SmsVerificationCodeNotVerifiedException",
"DataLanguage":"TR"
},
{
"DataKey":"Exception.Code.11",
"DataValue":"Geçersiz dil bilgisi.",
"DataDescription":"InvalidLanguageException",
"DataLanguage":"TR"
}
],
"ResultCode":"00",
"ResultMessage":"Success",
"Exception":null
}
对不起我的英文并感谢您的帮助。
答案 0 :(得分:1)
我已经使用JSONModel框架来了解最新情况。
请注意,请在项目中使用驼峰案例表示法。
模型类(在这种情况下只有头文件很重要):
<强> LanguageString.h 强>
#import "JSONModel.h"
@protocol LanguageString;
@interface LanguageString : JSONModel
@property (strong, nonatomic) NSString *DataKey;
@property (strong, nonatomic) NSString *DataValue;
@property (strong, nonatomic) NSString *DataDescription;
@property (strong, nonatomic) NSString *DataLanguage;
@end
<强> LanguagePack.h 强>
#import "JSONModel.h"
#import "LanguageString.h"
@interface LanguagePack : JSONModel
@property(strong,nonatomic) NSArray<LanguageString> *Data;
@end
<强> ResultObject 强>
#import <Foundation/Foundation.h>
#import "JSONModel.h"
#import "LanguageString.h"
@interface ResultObject : JSONModel
@property (strong, nonatomic) NSArray<LanguageString> *Data;
@property (strong, nonatomic) NSString *ResultCode;
@property (strong, nonatomic) NSString *ResultMessage;
@property (strong, nonatomic) NSObject<Optional> *Exception;
@end
然后运行:
NSString *json = @"{\"Data\":[{\"DataKey\":\"AppTemplate.CancelButton.Text\",\"DataValue\":\"Iptal\",\"DataDescription\":\"\",\"DataLanguage\":\"TR\"},{\"DataKey\":\"Exception.Code.7\",\"DataValue\":\"SMS dogrulama kodu hatali ya da zaman asimina ugramis.\",\"DataDescription\":\"SmsVerificationCodeNotVerifiedException\",\"DataLanguage\":\"TR\"},{\"DataKey\":\"Exception.Code.11\",\"DataValue\":\"Geçersiz dil bilgisi.\",\"DataDescription\":\"InvalidLanguageException\",\"DataLanguage\":\"TR\"}],\"ResultCode\":\"00\",\"ResultMessage\":\"Success\",\"Exception\":null}";
ResultObject *ro = [[ResultObject alloc] initWithString:json error:nil];
NSLog(@"ResultCode=%@, ResultMessage=%@", ro.ResultCode, ro.ResultMessage);
for (LanguageString *ls in ro.Data) {
NSLog(@"\n-----\nDataKey=%@\nDataValue=%@\nDataDescription=%@\nDataLanguage=%@\n-----", ls.DataKey, ls.DataValue, ls.DataDescription, ls.DataLanguage);
}
结果:
2014-01-24 14:46:31.050 Test[1420:70b] ResultCode=00, ResultMessage=Success
2014-01-24 14:46:31.052 Test[1420:70b]
-----
DataKey=AppTemplate.CancelButton.Text
DataValue=Iptal
DataDescription=
DataLanguage=TR
-----
2014-01-24 14:46:31.052 Test[1420:70b]
-----
DataKey=Exception.Code.7
DataValue=SMS dogrulama kodu hatali ya da zaman asimina ugramis.
DataDescription=SmsVerificationCodeNotVerifiedException
DataLanguage=TR
-----
2014-01-24 14:46:31.053 Test[1420:70b]
-----
DataKey=Exception.Code.11
DataValue=Geçersiz dil bilgisi.
DataDescription=InvalidLanguageException
DataLanguage=TR
-----
我希望它是您所期望的,ResultObject必须定义数组的类型,这是唯一的区别:
@property (strong, nonatomic) NSArray<LanguageString> *Data;
答案 1 :(得分:0)
提供自定义对象的JSON结构 - 如果没有它,我无法给出明确的答案。
至少,我可以为JSON推荐NSJSONSerialization
您可以从以下内容开始:
NSData *responseData; // insert your data here
NSDictionary *response = (NSDictionary*)[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];
NSLog(@"response :%@", response);
注意,代替'(NSDictionary *)'你可以使用'(NSArray *)' - 它取决于你的数据结构。
好的,所以我会告诉你如何使用NSJOSONSerlization解析它:
NSData* responseData = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *response = (NSDictionary*)[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];
NSLog(@"response :%@", response);
// Parse
NSString *resultCode = response[@"ResultCode"];
NSString *resultMessage = response[@"ResultMessage"];
NSArray *dataArr = response[@"Data"];
for (NSDictionary *item : dataArr) {
LanguageString *ln = [LanguageString new];
ln.dataKey = item[@"DataKey"];
ln.dataValue = item[@"DataValue"];
ln.dataDescription = item[@"DataDescription"];
ln.dataLanguage = item[@"DataLanguage"];
/* TODO: store 'ln' object in desired model */
}
当然假设,dataStr类似于:
NSString *dataStr = @"{\"Data\":[{\"DataKey\":\"AppTemplate.CancelButton.Text\",\"DataValue\":\"Iptal\",\"DataDescription\":\"\",\"DataLanguage\":\"TR\"},{\"DataKey\":\"Exception.Code.7\",\"DataValue\":\"SMS dogrulama kodu hatali ya da zaman asimina ugramis.\",\"DataDescription\":\"SmsVerificationCodeNotVerifiedException\",\"DataLanguage\":\"TR\"},{\"DataKey\":\"Exception.Code.11\",\"DataValue\":\"Geçersiz dil bilgisi.\",\"DataDescription\":\"InvalidLanguageException\",\"DataLanguage\":\"TR\"}],\"ResultCode\":\"00\",\"ResultMessage\":\"Success\",\"Exception\":null}";
请注意,这只是一个提示。还要考虑使用ResKit
这样的框架答案 2 :(得分:0)
JSON Feed中的“数据”键是一个数组。因此,您的“数据”属性也需要是一个数组而不是NSObject。
但是我想你已经知道了,因为你注意到它可行,如果你这样做