1

I want to remove first and last bracket from my 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" : "",
        }
      ]
    }

I want to remove only first and last { } from the above NSString.

Make sure its not Dictionary. I have this in NSString.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Sam Shaikh
  • 1,596
  • 6
  • 29
  • 53
  • 1
    Possible duplicate of [Replace a character at a certain index in NSString](http://stackoverflow.com/questions/8953044/replace-a-character-at-a-certain-index-in-nsstring) – ΦXocę 웃 Пepeúpa ツ Nov 30 '15 at 14:36
  • 2
    are the { } always the first and last characters in your string ? – enzo Nov 30 '15 at 14:36
  • 5
    this is your Json String/array bro , serlize this string you can get your value – Anbu.Karthik Nov 30 '15 at 14:37
  • There are a few ways you can solve this. If they are always the first and last characters of your string, just replace the first and last character in the index from @jipr311's link. Or you could get first object of the array... Or get the valueForKey of the "Questions" key – MSU_Bulldog Nov 30 '15 at 14:38
  • Removing `{` and `}` is not going to solve your problem, because `"Questions" : ` part is going to remain there. – Sergey Kalinichenko Nov 30 '15 at 14:40
  • 1
    It looks like you have perfectly fine JSON (which you probably converted from NSData, which was completely unnecessary), and now you want to convert it to some broken JSON. Next thing you tell us you are going to parse it by hand... Lookup NSJSONSerialization before you waste lots of time. – gnasher729 Nov 30 '15 at 17:23

1 Answers1

1

UPDATE

The option is serializing:

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"]);
    .....
}

UPDATE 2

But BE CAREFUL with your string. It's wrong. You have to check the commas after 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
    }
  ]
}

No commas have to go there.

nigelman
  • 153
  • 8
  • the second option is correct , remove the first choice -- thats is wrong bro..\ – Anbu.Karthik Nov 30 '15 at 14:53
  • Ok, I'm going ;). That was a quick answer :) – nigelman Nov 30 '15 at 15:01
  • This code is not going to work on the string shown in the question since that string is not a valid JSON array. – rmaddy Nov 30 '15 at 15:21
  • That's true. The error on the JSON string is the comma ',' after "media_type" : "" . I'm going to warn him – nigelman Nov 30 '15 at 15:35
  • @nigelman No, you misunderstand. The JSON isn't an array, it's a dictionary. Your code assumes the JSON is an array. The JSON is a dictionary with the "Questions" key. The value of that key is the array of dictionaries. You need to fix the code in your answer. – rmaddy Nov 30 '15 at 17:16
  • No @rmaddy. You misunderstood my code. In my first version, I put " [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil] " in a Dictionary and then, I took the "Questions" key as an array. Someone edited my code and joined both operations, and I didn't disagree. If you look carefully, you will see the " objectForKey:@"Questions"] " at the end. And of course is an Array. But this JSON as is, is incorrect due to these 2 commas. – nigelman Nov 30 '15 at 23:03
  • @nigelman Oops. Yes, sorry. I didn't see the call to `objectForKey:`. It was too far to the right to notice. It would have been better to split that up into multiple lines. It would make it easier to read and to debug. – rmaddy Nov 30 '15 at 23:06
  • That was something @Anbu.Karthik did. I initially wrote the code in 2 lines xD. I'm going to restore this 2 lines to better comprehension :) – nigelman Nov 30 '15 at 23:08