首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSJSONSerialization返回null

NSJSONSerialization返回null
EN

Stack Overflow用户
提问于 2012-12-18 14:34:15
回答 1查看 1.2K关注 0票数 0

下面列出的代码返回4项非常好,但是当我增加限制5的项数时,它返回null,有人能告诉我为什么它会这样吗?

代码语言:javascript
复制
NSString *hostStr = @"http://localhost:8888/iphone-so/product.json.php?";
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:hostStr]];
NSError *error;
product = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

product.json.php返回以下4个项目

代码语言:javascript
复制
{
    "products": {
        "19": [
            {
                "id": "19",
                "name": "Save $240 on your next photo session",
                "image_url": "http://localhost:8888/iphone-so/images/13558127351.jpg",
                "highlight": "This is a fantastic offer! You can save 63% with this offer the next time you need an on-site photographer."
            }
        ],
        "21": [
            {
                "id": "21",
                "name": "One Hour Massage",
                "image_url": "http://localhost:8888/iphone-so/images/13558127352.jpg",
                "highlight": "With this special offer receive one hour massage for $35. If you have ever wanted a massage after a long day, this is it! Buy one now for yourself or a loved one. You will save almost 70% with this o"
            }
        ],
        "22": [
            {
                "id": "22",
                "name": "Start your spring cleaning with this Offer! Get one area cleaned for Half-Price!",
                "image_url": "http://localhost:8888/iphone-so/images/13558127353.jpg",
                "highlight": "For only $40 you can save on having your carpet cleaned with this offer! Save 50% with this offer and receive a free gift."
            }
        ],
        "23": [
            {
                "id": "23",
                "name": "Let Their Creativity Unwined",
                "image_url": "http://localhost:8888/iphone-so/images/13558127354.jpg",
                "highlight": "For only $60 children can express themselves with art! With this offer you can see what creativity your child is keeping bottled up with this 2 hour class!"
            }
        ]
    }
}

这是包含5个项目的product.json.php结果

代码语言:javascript
复制
{
    "products": {
        "19": [
            {
                "id": "19",
                "name": "Save $240 on your next photo session",
                "image_url": "http://localhost:8888/iphone-so/images/13558151441.jpg",
                "highlight": "This is a fantastic offer! You can save 63% with this offer the next time you need an on-site photographer."
           }
        ],
        "21": [
            {
                "id": "21",
                "name": "One Hour Massage",
                "image_url": "http://localhost:8888/iphone-so/images/13558151442.jpg",
                "highlight": "With this special offer receive one hour massage for $35. If you have ever wanted a massage after a long day, this is it! Buy one now for yourself or a loved one. You will save almost 70% with this o"
            }
        ],
        "22": [
            {
                "id": "22",
                "name": "Start your spring cleaning with this Offer! Get one area cleaned for Half-Price!",
                "image_url": "http://localhost:8888/iphone-so/images/13558151443.jpg",
                "highlight": "For only $40 you can save on having your carpet cleaned with this offer! Save 50% with this offer and receive a free gift."
            }
        ],
        "23": [
            {
                "id": "23",
                "name": "Let Their Creativity Unwined",
                "image_url": "http://localhost:8888/iphone-so/images/13558151444.jpg",
                "highlight": "For only $60 children can express themselves with art! With this offer you can see what creativity your child is keeping bottled up with this 2 hour class!"
            }
        ],
        "24": [
            {
                "id": "24",
                "name": "Custom framing for only $49! An offer valued at $200",
                "image_url": "http://localhost:8888/iphone-so/images/13558151445.jpg",
                "highlight": "Framing doesn’t have to be expensive! Now with this offer you can get $200 worth of framing for only $49. Don’t let your art hang without a frame, take advantage of this offer. "
            }
        ]
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-19 04:10:30

来自NSJSONSerializationJSONObjectWithData:options:error:方法的文档:

数据必须采用JSON规范中列出的5种受支持的编码之一: UTF-8、UTF-16LE、UTF-16BE、UTF-32LE、UTF-32BE。

由于您似乎可以控制服务器源代码,因此您的URL是"http://localhost:8888/",下面的代码也适用:

UTF-8是用于解析的最有效的编码,因此,如果您可以选择对传递给此方法的数据进行编码,请使用

-8。

如果您确实需要不在UTF-8中的字符,请尝试使用UTF-16,或者将特殊字符编码为百分数转义。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13927571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档