0

I get the JSON format like this:

stream( { posts: [{CHANNEL: {ios: "(format=m3u8-aapl).m3u8"} }]})

What I want to get is an array for the "ios". This is my code:

id jsonObjects = [NSJSONSerialization JSONObjectWithData:
                  jsonSource options:NSJSONReadingMutableContainers error:nil];
for (NSDictionary *dataDict in jsonObjects) {
    NSArray *ios_data = [[[dataDict objectForKey:@"posts"] objectForKey:@"CHANNEL"] objectForKey:@"ios"];
    NSLog(@"%@",ios_data);
    dict = [NSDictionary dictionaryWithObjectsAndKeys:ios_data, ios,nil];

}

but it return in NULL, what the problem of it?

3
  • The above is not legal JSON, nor is it a representation of parsed JSON in iOS. Commented Feb 26, 2014 at 3:53
  • And JSONObjectWithData has an error: parameter. Make use of it!!! Commented Feb 26, 2014 at 3:55
  • (See json.org for a description of the JSON syntax. Mainly the above is missing quotes around the key names.) Commented Feb 26, 2014 at 3:57

3 Answers 3

3

Your "JSON":

stream( { posts: [{CHANNEL: {ios: "(format=m3u8-aapl).m3u8"} }]})

Is not JSON. You can try running it through a validator like http://jsonlint.com/ to test it out.

Also, you should create an NSError reference to pass in instead of nil so NSJSONSerialization can vend you an error object. This will help in your debugging.


Here is an example of what your data would look like if it were valid JSON:

{
    "stream": [
        {
            "posts": [
                {
                    "CHANNEL": {
                        "ios": "(format=m3u8-aapl).m3u8"
                    }
                }
            ]
        }
    ]
}

(I spaced it out to be more legible, but the spacing is unnecessary for parsing.)

Sign up to request clarification or add additional context in comments.

Comments

0

Once you have your array holding 'posts' you can drill down like this:

NSArray *fileData = [myDic1 valueForKeyPath:@"posts.CHANNEL"];

and then access like this:

for (int i=0; i < [clientFileData count]; i++) {
NSDictionary *myDictionary = [fileData objectAtIndex:i];
]

Also, unless I am wrong, your JSON file seems incorrectly formatted.

1 Comment

Thank you, it's about my callback json.
0

Get your response like:

NSDictionary *dictionaryData = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

Correct JSOn should be like this

{
   "posts":[
      {
         "CHANNEL":{
            "ios":"(format=m3u8-aapl).m3u8"
         }
      }
   ]
}

Get the correct format of JSON and then try to parse it. And one thing you were doing wrong is POST is an array.

1 Comment

Thank you every much Rashad, i think i will request some third party to fixed the format or request some another way to parse the data of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.