2015-08-08 63 views
-1

我用的iOS转换Facebook的图形JSON结果的NSMutableArray

if ([FBSDKAccessToken currentAccessToken]) { 
     [[[FBSDKGraphRequest alloc] initWithGraphPath:@"/v2.3/116178611870507/feed?limit=2" parameters:[NSMutableDictionary dictionaryWithObject:@"id, message, picture" forKey:@"fields"]] 
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { 
      if (!error) { 
       NSLog(@"fetched user:%@", result); 


      } 
      else { 
       NSLog(@"%@", error); 
      } 
     }]; 
    } 

,并得到:

{ 
    data =  (
       { 
      "created_time" = "2015-08-08T13:19:59+0000"; 
      id = "FirstID"; 
      message = "First Message"; 
     }, 
       { 
      "created_time" = "2015-08-07T13:34:20+0000"; 
      id = "THE ID"; 
      message = "Message Content"; 
     } 
    ); 

我想进入一个NSMutableArray本,以便更容易创建的tableview的,使用这种数据。我如何去转换它,因为它包含很多非UTF-8,如\u2019

以下是我为打开图形请求获取的非编辑结果代码。如您所见,有许多非UTF-8元素,例如/u2019而不是'。我只需要把它放到一个数组中,同时转换所有这些元素。:

{ 
    data =  (
       { 
      "created_time" = "2015-08-08T13:19:59+0000"; 
      id = "116178611870507_533557810132583"; 
      message = "10 HABITS OF HAPPY COUPLES\n\nWhat does it take to be happy in a relationship? If you\U2019re working to improve your marriage, here are the 10 habits of happy couples.\n\n1. GO TO BED AT THE SAME TIME. Remember the beginning of your relationship, when you couldn\U2019t wait to go to bed with each other to make love? Happy couples resist the temptation to go to bed at different times. They go to bed at the same time, even if one partner wakes up later to do things while their partner sleeps. And when their skins touch it still causes each of them to tingle and unless one or both are completely exhausted to feel sexually excited.\n\n2. CULTIVATE COMMON INTERESTS. After the passion settles down, it\U2019s common to realize that you have few interests in common. But don\U2019t minimize the importance of activities you can do together that you both enjoy. If common interests are not present, happy couples develop them. At the same time, be sure to cultivate interests of your own; this will make you more interesting to your mate and prevent you from appearing too dependent.\n\n3. WALK HAND IN HAND OR SIDE BY SIDE. Do things together. Rather than one partner lagging or dragging behind the other, happy couples walk comfortably hand in hand or side by side. They know it\U2019s more important to be with their spouse than to see the sights along the way.\n\n4. MAKE TRUST AND FORGIVENESS YOUR DEFAULT MODE. If and when they have a disagreement or argument, and if they can\U2019t resolve it, happy couples default to trusting and forgiving rather than distrusting and begrudging.\n\n5. FOCUS MORE ON WHAT YOUR SPOUSE DOES RIGHT THAN WHAT HE OR SHE DOES WRONG. If you look for things your partner does wrong, you can always find something. If you look for what he or she does right, you can always find something, too. It all depends on what you want to look for. Happy couples accentuate the positive.\n\n6. HUG EACH OTHER AS SOON AS YOU SEE EACH OTHER AFTER WORK. Our skin has a memory of \U201cgood touch\U201d (loved), \U201cbad touch\U201d (abused) and \U201cno touch\U201d (neglected). Couples who say hello with a hug keep their skin bathed in the \U201cgood touch,\U201d which can inoculate your spirit against anonymity in the world.\n\n7. SAY \"I LOVE YOU\" AND \"HAVE A GOOD DAY\" EVERY MORNING. This is a great way to buy some patience and tolerance as each partner sets out each day to battle traffic jams, long lines and other annoyances.\n\n8. SAY \"GOOD NIGHT\" EVERY NIGHT, REGARDLESS OF HOW YOU FEEL. This tells your partner that, regardless of how upset you are with him or her, you still want to be in the relationship. It says that what you and your partner have is bigger than any single upsetting incident.\n\n9. DO A \"WEATHER\" CHECK DURING THE DAY. Call your spouse at home or at work to see how his or her day is going. This is a great way to adjust expectations so that you\U2019re more in sync when you connect after work. For instance, if your partner is having an awful day, it might be unreasonable to expect him or her to be enthusiastic about something good that happened to you.\n\n10. BE PROUD TO BE SEEN WITH YOUR SPOUSE. Happy couples are pleased to be seen together and are often in some kind of affectionate contact \U2014 hand on hand or hand on shoulder or knee or back of neck. They are not showing off but rather just saying that they belong with each other.\n\nHappy couples have different habits than unhappy couples. A habit is a discrete behavior that you do automatically and that takes little effort to maintain. It takes 21 days of daily repetition of a new a behavior to become a habit. So select one of the behaviors in the list above to do for 21 days and voila, it will become a habit\U2026and make you happier as a couple. And if you fall off the wagon, don\U2019t despair, just apologize to your partner, ask their forgiveness and recommit yourself to getting back in the habit. ~ Dr. Mark Goulson"; 
     }, 
       { 
      "created_time" = "2015-08-07T13:34:20+0000"; 
      id = "116178611870507_533189820169382"; 
      message = "31 DUMB WAYS TO KILL YOUR MARRIAGE: \n\nThere are probably hundreds of dumb things to do in your marriage, but here are 31 that will destroy your marriage\U2026\n\n1. Believe that your marriage will be great without having to invest time or effort into it.\n\n2. NEVER compliment your spouse.\n\n3. Stop pursuing your spouse like you did before marriage.\n\n4. Be critical all the time.\n\n5. Demand that your spouse meets your needs.\n\n6. Pout when you don\U2019t get your way.\n\n7. Consider sex a chore and only do it because you feel obligated.\n\n8. Use the silent treatment when you are mad.\n\n9. Get angry \U2026 often.\n\n10. Flirt with people other than your spouse.\n\n11. Tell your spouse they need to make changes, but you make no effort to change.\n\n12. Never be willing to meet your spouse\U2019s needs.\n\n13. Make no effort to improve yourself or your marriage.\n\n14. Speak negatively about your spouse around other people.\n\n15. Make no effort to keep yourself \U201clooking good\U201d for your spouse.\n\n16. Use derogatory words like stupid, dumb, ignorant and idiot.\n\n17. Always blame your spouse for the problems. It\U2019s never your fault.\n\n18. Take your mate for granted.\n\n19. Quit trying to impress your spouse (you\U2019re married, it\U2019s not needed anymore).\n\n20. Withhold sex.\n\n21. Never take any time to get away without the kids.\n\n22. Don\U2019t tell your spouse how much you love or appreciate them. Just expect them to know it.\n\n23. Be dishonest.\n\n24. When arguing \U2026 bring up old stuff from the past.\n\n25. Have a short fuse.\n\n26. Feel that God is an option in your marriage, not an essential.\n\n27. When angry be verbally abusive.\n\n28. Never seek outside help when you have problems.\n\n29. Have an \U201cit\U2019s my way or the highway\U201d attitude.\n\n30. Spend more time with your friends than your spouse.\n\n31. Never say, \U201cI love you."; 
     } 
    ); 

这是我的完整代码。由于有2篇文章,我希望在我的NSMutableArray中有2个对象,并且对于数组中的每个对象,使用RSSEntry类进行排序并从中创建一个新的NSMutableArray。但是,我只在我创建的NSMutableArray中获得1个项目。

if ([FBSDKAccessToken currentAccessToken]) { 
     [[[FBSDKGraphRequest alloc] initWithGraphPath:@"/v2.3/116178611870507/feed?limit=2" parameters:[NSMutableDictionary dictionaryWithObject:@"id, message, picture" forKey:@"fields"]] 
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { 
      if (!error) { 
       // NSLog(@"fetched user:%@", result); 
       self.arrayName = [[NSMutableArray alloc] init]; 
       [self.arrayName addObject:[result objectForKey:@"data"]]; 
       // NSLog(@"%@", self.arrayName); 
       int i; 
       NSLog(@"%lu", (unsigned long)[self.arrayName count]); 
       for (i = 0; i < [self.arrayName count]; i++) { 
        id myArrayElement = [self.arrayName objectAtIndex:i]; 
        NSString *theMessage = [myArrayElement valueForKey:@"message"]; 
        // NSLog(@"%@", theMessage); 
        RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:@"Test" 

                   articleTitle:@"Testing" 
                   articleUrl:@"TestingURL" 
                   articleDate:[NSDate date] 
                   articleImage:theMessage] autorelease]; 
        [self.theEntries addObject:entry]; 

         } 
       NSLog(@"%@", self.theEntries); 

      } 
      else { 
       NSLog(@"%@", error); 
      } 
     }]; 
    } 
+0

更新你的问题:

您可以通过使用更现代的语法,还可以提高你的代码。你所指的'\ u2019'角色在哪里?它与创建数组的需求有什么关系? – rmaddy

+0

@rmaddy增加了一些更新。 – user717452

+0

由于文本的记录方式,Unicode转义符就在那里。除非它们出现在用户身上,否则不要担心它们。再次显示你尝试从你得到的数据中创建一个数组。解释你有什么麻烦,并显示数组应该是什么样子。 – rmaddy

回答

1

的问题是这两条线:

self.arrayName = [[NSMutableArray alloc] init]; 
[self.arrayName addObject:[result objectForKey:@"data"]]; 

您创建一个数组,然后添加一个数组到一个([result objectForKey:@"data"]返回数组)。

替换为这两条线:

self.arrayName = [result[@"data"] mutableCopy]; 

现在self.arrayName将包含与所述两个期望词典的阵列。用你想要的一个例子,你试图代码来创建数组

if (!error) { 
    // NSLog(@"fetched user:%@", result); 
    self.arrayName = [result[@"data"] mutableCopy]; 
    // NSLog(@"%@", self.arrayName); 
    NSLog(@"%lu", (unsigned long)self.arrayName.count); 
    for (NSDictionary *myArrayElement in self.arrayName) { 
     NSString *theMessage = myArrayElement[@"message"]; 
     // NSLog(@"%@", theMessage); 
     RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:@"Test" 
           articleTitle:@"Testing" 
           articleUrl:@"TestingURL" 
           articleDate:[NSDate date] 
           articleImage:theMessage] autorelease]; 
     [self.theEntries addObject:entry]; 
    } 
    NSLog(@"%@", self.theEntries); 
} else { 
    NSLog(@"%@", error); 
}