我想从NSMutableDictionary获得价值。
这是我的字典:
{
"Balloon" = {
"fields_id" = 1;
"fields_list" = "Balloon";
"posm_details" = "<NBPosm_Sub: 0x828d220>";
};
"Booklet" = {
"fields_id" = 2;
"fields_list" = "Booklet";
"posm_details" = "<NBPosm_Sub: 0x828d7b0>";
};
"Brochure" = {
"fields_id" = 3;
"fields_list" = "Brochure";
"posm_details" = "<NBPosm_Sub: 0x828d890>";
};
"Fact tags - 1" = {
"fields_id" = 4;
"fields_list" = "Fact tags - 1";
"posm_details" = "<NBPosm_Sub: 0x828b760>";
};
"Fact tags - 10" = {
"fields_id" = 13;
"fields_list" = "Fact tags - 10";
"posm_details" = "<NBPosm_Sub: 0x828ab90>";
};
}
以上NBPosm_Sub是我的一个自定义类,即
NBPosm_Sub.h :-
#import <Foundation/Foundation.h>
@interface NBPosm_Sub : NSObject
@property (nonatomic, retain) NSString *posmId;
@property (nonatomic, retain) NSString *posmName;
@property (nonatomic, retain) NSString *posmQuantity;
@property (nonatomic, retain) NSString *posmRemarks;
@property (nonatomic, retain) NSString *posmAfterImageId;
@property (nonatomic, retain) NSString *posmBeforeImageId;
@property (nonatomic, retain) NSMutableDictionary *dictPosmDetails;
@end
NBPosm_Sub.m :-
#import "NBPosm_Sub.h"
@implementation NBPosm_Sub
@synthesize posmId, posmName, posmQuantity, posmRemarks, posmAfterImageId, posmBeforeImageId, dictPosmDetails;
- (id) initWithCoder: (NSCoder *)coder
{
self = [[NBPosm_Sub alloc] init];
if (self != nil)
{
self.posmId = [coder decodeObjectForKey:@"posmId"];
self.posmName = [coder decodeObjectForKey:@"Name"];
self.posmQuantity = [coder decodeObjectForKey:@"Quantity"];
self.posmRemarks = [coder decodeObjectForKey:@"Remarks"];
self.posmAfterImageId = [coder decodeObjectForKey:@"Picture2Id"];//after
self.posmBeforeImageId = [coder decodeObjectForKey:@"Picture1Id"];//before
self.dictPosmDetails=[coder decodeObjectForKey:@"dictPosmDetails"];
}
return self;
}
- (void)encodeWithCoder: (NSCoder *)coder
{
[coder encodeObject:posmId forKey:@"posmId"];
[coder encodeObject:posmName forKey:@"Name"];
[coder encodeObject:posmQuantity forKey:@"Quantity"];
[coder encodeObject:posmRemarks forKey:@"Remarks"];
[coder encodeObject:posmAfterImageId forKey:@"Picture2Id"];//after
[coder encodeObject:posmBeforeImageId forKey:@"Picture1Id"];//before
[coder encodeObject:dictPosmDetails forKey:@"dictPosmDetails"];
}
@end
你能帮我从字典中获取关键值吗?感谢
如何使我的字典输出应该像:
{
{
"fields_id" = 1;
"fields_list" = "Balloon";
"posm_details" = "<NBPosm_Sub: 0x828d220>";
};
{
"fields_id" = 2;
"fields_list" = "Booklet";
"posm_details" = "<NBPosm_Sub: 0x828d7b0>";
};
{
"fields_id" = 3;
"fields_list" = "Brochure";
"posm_details" = "<NBPosm_Sub: 0x828d890>";
};
{
"fields_id" = 4;
"fields_list" = "Fact tags - 1";
"posm_details" = "<NBPosm_Sub: 0x828b760>";
};
{
"fields_id" = 13;
"fields_list" = "Fact tags - 10";
"posm_details" = "<NBPosm_Sub: 0x828ab90>";
};
}
如何从所有范围中删除标题?
答案 0 :(得分:0)
在代码下面检查一次...
在dict的位置使用包含数据的字典:
NSString *strid = [[dict objectForKey:@"Fact tags"] objectForKey:@"fields_id"];
NSLog(@"str = %@", strid);
或者你想让列表完整字典使用代码,但你必须为字符串或任何需要的地方赋值。
NSString *str = [NSString stringWithFormat:@"Fact tags\nfields_id=%@\nfields_list=%@\nposm_details=%@", [[dict objectForKey:@"Fact tags"] objectForKey:@"fields_id"], [[dict objectForKey:@"Fact tags"] objectForKey:@"fields_list"], [[dict objectForKey:@"Fact tags"] objectForKey:@"posm_details"]];
NSLog(@"%@", str);
答案 1 :(得分:-2)
试试此代码
id obj1 = [arr(yourresponce) objectAtIndex:0];
id obj2 = [obj1 valueForKey:@"Balloon"];
for(int i=0; i <[obj2 count]; i++)
{
NSMutableDictionary *dict = [obj2 objectAtIndex:i];
NSString *str = [dict objectForKey:@"fields_id"];
}