【问题标题】:How to get event_id from this JSON如何从此 JSON 中获取 event_id
【发布时间】:2014-04-24 06:44:24
【问题描述】:

我正在研究检索 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。我有什么办法可以解决这个问题。

【问题讨论】:

  • 检查您的回复。它的 json 无效。
  • 您好 Chinttu 先生,感谢您的回复。你是什​​么意思它不是一个有效的json
  • 得到正确的json响应[[[[Response objectForKey:@"notification"] objectAtIndex:0]objectForKey:@"custom_prop" ] valueForKey:@"event_id"]后就可以得到eventid了。
  • 你现在使用的代码是什么? @ChinttuRoxeNRamanian 的回答应该有效。
  • 它不是有效的 JSON,因为这里:"custom_prop":"{ ... }" 它应该是:"custom_prop":{ ... }(大括号周围没有引号)

标签: ios objective-c json


【解决方案1】:

试试这个:

   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);
        }

    }

【讨论】:

    【解决方案2】:

    所以我不是来自 .net 的 Objective-c 世界,但我猜解决方案是一样的,你有 2 种方法来解决这个问题: 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.将其序列化为动态对象(我不喜欢这种方法,因为它会离开你 有足够的空间来处理异常和错误)。

    希望这能帮助您找到正确的方向。

    【讨论】:

      猜你喜欢
      • 2020-10-03
      • 1970-01-01
      • 2016-09-16
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-06
      相关资源
      最近更新 更多