如何删除iOS中的NSString中的第一个和最后一个{}?

时间:2015-11-30 14:32:32

标签: ios objective-c nsstring

我想从我的NSString中删除第一个和最后一个括号。

{
    "Questions" : [
        {
          "title" : "This is my Question",
          "question_id" : "123123123213",
          "answers" : [
            "correct answer 1",
            "wrong answer 1",
            "wrong answer 2",
            "wrong answer 3"
          ],
          "media_type" : "",
        },
    {
          "title" : "This is my Question",
          "question_id" : "2342342342342",
          "answers" : [
            "correct answer 1",
            "wrong answer 1",
            "wrong answer 2",
            "wrong answer 3"
          ],
          "media_type" : "",
        }
      ]
    }

我想从上面的{中删除第一个和最后一个} NSString

确保其不是Dictionary。我在NSString中有这个。

1 个答案:

答案 0 :(得分:1)

<强>更新

该选项正在序列化:

NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *dictJson = [NSJSONSerialization JSONObjectWithData:data
                                                     options:kNilOptions
                                                       error:nil];

NSArray *arr = dictJson[@"Questions"];

for (NSDictionary *dict in arr)
{ 
    NSLog(@"title ==%@",dict[@"title"]);
    .....
}

更新2

但要小心你的字符串。这是不对的。您必须在media_type:

之后检查逗号
{
"Questions" : [
    {
      "title" : "This is my Question",
      "question_id" : "123123123213",
      "answers" : [
        "correct answer 1",
        "wrong answer 1",
        "wrong answer 2",
        "wrong answer 3"
      ],
      "media_type" : ""**,** //THIS
    },
{
      "title" : "This is my Question",
      "question_id" : "2342342342342",
      "answers" : [
        "correct answer 1",
        "wrong answer 1",
        "wrong answer 2",
        "wrong answer 3"
      ],
      "media_type" : ""**,** //AND THIS
    }
  ]
}

没有逗号必须去那里。