0

我正在尝试发送一系列字典,但验证失败。这是一个示例输出:

  {
        album = "Karaoke - In the style of Goo Goo Dolls - Vol. 2";
        artist = "Stingray Music (Karaoke)";
        length = "208.404";
        title = "Better Days (Karaoke Version)";
    },
        {
        album = Down;
        artist = "Jay Sean";
        length = "212.61";
        title = Down;
    },
        {
        album = "Growing Pains";
        artist = "Mary J Blige";
        length = "301.844";
        title = "Come to Me (Peace)";
    }

这是生成它的代码:

NSMutableArray *mutableSongsToSerialize = [NSMutableArray array];
NSArray *songs = [playlist items];              
for (MPMediaItem *song in songs){
       NSString *title =[song valueForProperty: MPMediaItemPropertyTitle];
       NSString *artist =[song valueForProperty: MPMediaItemPropertyAlbumArtist];
       NSString *album =[song valueForProperty: MPMediaItemPropertyAlbumTitle];
       NSString *length =[song valueForProperty: MPMediaItemPropertyPlaybackDuration];
       NSDictionary *songDictionary = @{@"title": title, @"artist": artist, @"album":album, @"length":length};
       [mutableSongsToSerialize addObject:songDictionary];
}
NSString *jsonRepresentation = [NSJSONSerialization JSONOBjectWithData:mutableSongsToSerialize options:0 error:NULL];

我认为这条线需要以某种方式进行修改,以便每首歌曲都不会被视为根元素,但我不确定具体要做什么。

NSDictionary *songDictionary = @{@"title": 标题, @"artist": 艺术家, @"album":album, @"length":length};

4

2 回答 2

2

你的 JSON 是无效的,你应该把:而不是=,而不是;,不要忘记省略最后一个'。此外,必须引用哈希键:

{
    "album": "Karaoke - In the style of Goo Goo Dolls - Vol. 2",
    "artist": "Stingray Music (Karaoke)",
    "length": "208.404",
    "title": "Better Days (Karaoke Version)"
},
...

您还可以使用JSON lint来检查您的 JSON。

于 2013-08-21T21:57:17.977 回答
2

您没有使用正确的方法来序列化您的 JSON。你需要使用+[NSJSONSerialization dataWithJSONObject:options:error].

这是一个使用与您的结构相同但具有硬编码值的示例:

NSDictionary *song1 = @{@"title": @"title1", @"artist": @"artist1", @"album":@"album1", @"length":@"length1"};
NSDictionary *song2 = @{@"title": @"title2", @"artist": @"artist2", @"album":@"album2", @"length":@"length2"};
NSDictionary *song3 = @{@"title": @"title3", @"artist": @"artist3", @"album":@"album3", @"length":@"length3"};
NSArray *songs = @[song1, song2, song3];
NSError *error = nil;
NSData *JSONData = [NSJSONSerialization dataWithJSONObject:songs options:NSJSONWritingPrettyPrinted error:&error];
NSString *JSONDataAsString = [[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding];
NSLog(@"JSON String = %@",JSONDataAsString);
NSLog(@"error was %@",error);

输出如下所示:

JSON String = [
  {
    "title" : "title1",
    "album" : "album1",
    "length" : "length1",
    "artist" : "artist1"
  },
  {
    "title" : "title2",
    "album" : "album2",
    "length" : "length2",
    "artist" : "artist2"
  },
  {
    "title" : "title3",
    "album" : "album3",
    "length" : "length3",
    "artist" : "artist3"
  }
]

如果您不想添加 Objective-C 代码来查看字符串输出,您可以在 lldb 中尝试以下操作:

expr (NSString *) [[NSString alloc] initWithData:JSONData encoding:4]

4用于代替NSUTF8StringEncoding由于调试器的可见性问题。

于 2013-08-21T22:14:43.907 回答