0

以下にリストされているコードは4つのアイテムを非常によく返しますが、アイテムの数を増やして制限5とすると、nullが返されます。なぜこのように動作するのか、誰かに教えてもらえますか?

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つのアイテムで次を返します

{
    "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の結果です。

{
    "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. "
            }
        ]
    }
}
4

1 に答える 1

1

NSJSONSerializationJSONObjectWithData:options:error:メソッドのドキュメントから:

データは、JSON仕様にリストされているサポートされている5つのエンコーディング(UTF-8、UTF-16LE、UTF-16BE、UTF-32LE、UTF-32BE)のいずれかである必要があります。

サーバーのソースコードを制御しているように見えるので、URLは " http://localhost:8888/"です。次のビットも適用されます:

解析に使用する最も効率的なエンコードはUTF-8であるため、このメソッドに渡されるデータのエンコードを選択できる場合は、UTF-8を使用してください。

UTF-8tryに含まれていない文字が本当に必要な場合UTF-16、または特殊文字をパーセントエスケープでエンコードする場合。

于 2012-12-18T20:10:30.430 に答える