[英]How to I get the info from a dictionary that I got from JSON - Objective-C/IOS/xCode
I am currently getting a dictionary with json info that I call json. 我目前正在得到一个带有json信息的字典,我称之为json。
NSDictionary* json=[NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
I am then getting the Journals part of it with this code: 然后,我使用以下代码来获取Journals部分:
NSDictionary *journalJSON = [json objectForKey:@"journals"];
Everything so far is turing out as expected. 到目前为止,一切都在按预期进行。 But if I do
NSLog(@"Journals: %@", journalJSON);
但是如果我这样做
NSLog(@"Journals: %@", journalJSON);
it returns this: 它返回此:
Journals: (
{
journalDate = "0000-00-00";
journalName = Test;
"users_userID" = 2;
}
)
So how do I go about getting that info? 那么我该如何获取该信息? I want to store each one in a variable such as: self.journals.journalName = journalName;
我想将每个变量存储在一个变量中,例如:self.journals.journalName = journalName; (I already have these variables named and working. Just need to know how to get the info from the Journals variable into it)
(我已经将这些变量命名并工作了。只需要知道如何从Journals变量中获取信息即可)
I'm pulling this info from a MYSQL database through php so there might actually be more than one journal....and I'd want to loop through and save each one into the class. 我正在通过php从MYSQL数据库中获取此信息,因此实际上可能有多个日记本...。我想循环遍历并将每个日记本保存到类中。
Right now I don't have a loop really and am doing this...though it's not right but hopefully you get the idea of what I want to accomplish: 现在,我确实没有循环并且正在执行此操作……尽管它不正确,但是希望您对我想要完成的事情有所了解:
journal *thisJournal = [[journal alloc]init];
thisJournal.name = name;
thisJournal.date = @"12/25/1981";
[self.journals addObject:thisJournal];
I hope this all makes sense. 我希望这一切都有意义。 I know I am close to the right answer because I can see the data.
我知道我接近正确的答案,因为我可以看到数据。 Just not sure how to parse it at this point.
只是不确定现在如何解析它。
THanks in advance! 提前致谢! Rob
抢
You are very close, try this: 您非常接近,请尝试以下操作:
journal *thisJournal = [[journal alloc]init];
for(NSDictionary *journal in journalJSON) {
thisJournal.name = [journal objectForKey: @"journalName"];
thisJournal.date = [journal objectForKey: @"journalDate"];
[self.journals addObject:thisJournal];
thisJournal = nil;
}
Just create a subclass ob NSObject named Journal (by convention, all ObjC classes start with a capital letter) and give it some properties (name, date, etc.), and then do a foreach on your deserialized JSON (which btw is no dictionary but an NSArray of NSDictionaries), get the values out of the current dictionary and create a new Journal object with them, then store that in some kind of mutable array or set again. 只需创建一个名为Journal的子类ob NSObject(按照惯例,所有ObjC类都以大写字母开头)并为其提供一些属性(名称,日期等),然后对反序列化的JSON进行foreach(顺便说一句,它没有字典)但要使用NSDictionaries的NSArray),请从当前字典中获取值并使用它们创建一个新的Journal对象,然后将其存储在某种可变数组中或再次进行设置。
I am sorry if this sounds rude, but it seems that you have hardly any experience with ObjectiveC and Cocoa touch. 很抱歉,这听起来很粗鲁,但是似乎您对ObjectiveC和Cocoa touch几乎没有任何经验。 I would suggest, that you do not try to learn it all by yourself, but watch the Stanford CS193p courses on iTunes U to get started.
我建议您不要尝试独自学习所有内容,而应在iTunes U上观看Stanford CS193p课程以开始使用。 After watching the first couple of those lectures, you will have a good basic understanding of how you accomplish what, and can start diving into advanced stuff.
看完前几节课之后,您将对如何完成工作有很好的基本了解,并可以开始研究高级内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.