简体   繁体   English

EXC_BAD_ACCESS,同时使用SBJSON解析JSON。 相应的消息状态变量json_string不是CFString

[英]EXC_BAD_ACCESS whilst parsing JSON using SBJSON. Corresponding message states variable json_string is not a CFString

I have the following response from server side: 我从服务器端收到以下响应:

{"_playLists":[{"name":"Playlist 1","items":[{"name":"Poza 1","target":"http:\/\/myaudi.fr","url":"http:\/\/test.res-novae.fr\/sfrplay\/upload\/image\/pic1_iphone3.jpg","url_thumb":"http:\/\/test.res-novae.fr\/sfrplay\/upload\/thumb\/pic1_iphone3_thumb.jpg"},{"name":"Poza 2","target":"http:\/\/audifrance.fr","url":"http:\/\/test.res-novae.fr\/sfrplay\/upload\/image\/pic2_iphone3.jpg","url_thumb":"http:\/\/test.res-novae.fr\/sfrplay\/upload\/thumb\/pic2_iphone3_thumb.jpg"}]},{"name":"Playlist 2","items":[{"name":"Poza 3","target":"http:\/\/google.ro","url":null,"url_thumb":null}]}]}

And I'm trying to acces this by using: 我正在尝试通过使用以下方法来对此进行访问:

SBJSON *parser = [[SBJSON alloc] init];
NSString *responseString = [request responseString];

NSString *json_string = [[NSString alloc] initWithData:responseString encoding:NSUTF8StringEncoding];
NSArray *statuses = [parser objectWithString:json_string error:nil];

But I get EXC_BAD_ACCESS at this line 但是我在这行得到EXC_BAD_ACCESS

 NSString *json_string = [[NSString alloc] initWithData:responseString encoding:NSUTF8StringEncoding]; 

saying variable json_string is not a CFString . 说变量json_string不是CFString

Can someone help me solve this and tell me how to act further to acces the JSON components?Thank you:) 有人可以帮我解决这个问题,并告诉我如何进一步采取行动以访问JSON组件吗?谢谢:)

EDIT: 编辑:

{
        items =         (
                        {
                name = "Poza 1";
                target = "http://myaudi.fr";
                url = "http://test.res-novae.fr/sfrplay/upload/image/pic1_iphone3.jpg";
                "url_thumb" = "http://test.res-novae.fr/sfrplay/upload/thumb/pic1_iphone3_thumb.jpg";
            },
                        {
                name = "Poza 2";
                target = "http://audifrance.fr";
                url = "http://test.res-novae.fr/sfrplay/upload/image/pic2_iphone3.jpg";
                "url_thumb" = "http://test.res-novae.fr/sfrplay/upload/thumb/pic2_iphone3_thumb.jpg";
            }
        );
        name = "Playlist 1";
    },
        {
        items =         (
                        {
                name = "Poza 3";
                target = "http://google.ro";
                url = "<null>";
                "url_thumb" = "<null>";
            }
        );
        name = "Playlist 2";
    }

You should use one of the JSON frameworks available out there, ie JSONKit or json-framework , which both makes it really easy to convert strings to JSON objects (ie an NSDictionary ). 您应该使用一种可用的JSON框架,即JSONKitjson-framework ,这两种方法都使将字符串转换为JSON对象(即NSDictionary )非常容易。

If you're using json-framework, you'd only have to do the following (if you've included JSON.h ): 如果您使用的是json-framework,则只需执行以下操作(如果已包含JSON.h ):

NSDictionary *jsonObject = [responseString JSONValue];

You do not need this line 您不需要此行

NSString *json_string = [[NSString alloc] initWithData:responseString encoding:NSUTF8StringEncoding];

responseString is already a string (and initWithData: expects a NSData object not NSString). responseString已经是一个字符串(并且initWithData:需要一个NSData对象而不是NSString)。 Just feed it straight to the parser: 只需将其直接输入解析器即可:

NSArray *statuses = [parser objectWithString:responseString error:nil];

Can someone help me solve this and tell me how to act further to acces the JSON components? 有人可以帮我解决这个问题,并告诉我如何进一步采取行动以访问JSON组件吗?

You now have a nice NSArray of NSDictionary objects representing your JSON data, you can access the first item, represented by 现在,您有了一个不错的NSDictionary对象的NSArray,它表示您的JSON数据,您可以访问由表示的第一项

{"name":"Playlist 1","items":[{"name":"Poza 1","target":"http:\/\/myaudi.fr","url":"http:\/\/test.res-novae.fr\/sfrplay\/upload\/image\/pic1_iphone3.jpg","url_thumb":"http:\/\/test.res-novae.fr\/sfrplay\/upload\/thumb\/pic1_iphone3_thumb.jpg"},{"name":"Poza 2","target":"http:\/\/audifrance.fr","url":"http:\/\/test.res-novae.fr\/sfrplay\/upload\/image\/pic2_iphone3.jpg","url_thumb":"http:\/\/test.res-novae.fr\/sfrplay\/upload\/thumb\/pic2_iphone3_thumb.jpg"}]}

by getting the first element in the array 通过获取数组中的第一个元素

NSDictionary* dict = [statuses objectAtIndex:0];

and the keys in the dictionary should be "name", and "items". 并且字典中的键应为“名称”和“项目”。 The object for "name" will be an NSString and the object for "items" will be an NSArray containing further NSDictionary objects describing each item. “名称”的对象将是NSString,“项目”的对象将是NSArray,其中包含描述每个项目的更多NSDictionary对象。

There is a issue with your line 您的电话线有问题

NSString *json_string = [[NSString alloc] initWithData:responseString encoding:NSUTF8StringEncoding];

you have declared responseString as NSString *responseString = [request responseString]; 您已将responseString声明为NSString *responseString = [request responseString]; and you are giving it as Data while allocating json_string 并且您在分配json_string时将其作为数据json_string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM