繁体   English   中英

如何从此 JSON 获取 event_id

[英]How to get event_id from this JSON

我正在研究检索 JSON 数据的 API,如下所示:

{"status":1,
"notification":
       [
        {
         "id":"175",
         "from_user":"3",
         "to_user":"7",
         "content":"Azri invited you to join the  Kaptr.",
         "is_read":"0",
         "type":"20",
         "created_date":"2014-04-17 04:11:02",
         "custom_prop":"{"status":1,
                         "api_name":"event_invite",
                         "event_id":155,
                         "event_name":false,
                         "invitee":"7"
                        }",
        "from_user_avatar_url":"http:\/\/app.kaptr.co\/vs\/kaptr_app\
        /images\/profile\/3\/1cAR7QSu.jpg"
         }
        ]
       }

我试图在custom_prop对象下获取event_id 有没有办法让我解决这个问题。

尝试这个 :

   NSString *da = [NSString stringWithFormat:@"{\"status\": \"1\",\"notification\": [{\"id\": \"175\",\"from_user\": \"3\",\"to_user\": \"7\",\"content\": \"Azri invited you to join the  Kaptr.\",\"is_read\": \"0\",\"type\": \"20\",\"created_date\": \"2014-04-17 04:11:02\",\"custom_prop\": [{\"status\": \"1\",\"api_name\": \"event_invite\",\"event_id\": \"155\",\"event_name\": \"false\",\"invitee\": \"7\"}],\"from_user_avatar_url\": \"string UTl\"}]}"];


    NSData* data1 = [da dataUsingEncoding:NSUTF8StringEncoding];

    NSString * strdata =[[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];

    NSLog(@"responseone  %@", strdata);

    NSError *e = nil;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data1 options:kNilOptions error:&e];
    NSArray *myArray = [json objectForKey:@"notification"];

    for (NSDictionary *dict in myArray)
    {
        NSArray *myArray2 = [dict objectForKey:@"custom_prop"];

        for (NSDictionary *dict in myArray2)
        {
            NSString *eventId = [dict objectForKey:@"event_id"];
            NSLog(@"eventId = %@", eventId);
        }

    }

所以我不是来自objective-c世界我来自.net,但我猜解决方案是一样的,你有两种方法来解决这个问题:1.创建一个代表这个JSON的对象,如:

      class SomeClass{
      public string Status {get;set;}
      public NotificationEnt[] {get;set;}
      }
      class NotificationEnt{
      public CustomProp {get;set;}
      }
      class CustomProp {
      public string event_Id {get;set;}
      }

并使用您的 JSON 序列化对象对其进行序列化(我确定您有所了解)

或 2. 将其序列化为动态对象(我不喜欢这种方法,因为它会给您留下足够的空间来处理异常和错误)。

希望这有助于使您朝着正确的方向前进。

暂无
暂无

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

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