I'm developing an iPhone application with latest SDK and XCode 4.2.
I'm trying to parse a JSON response. It is the first time I do it and I'm not sure which is the correct format from a JSON response.
From a web service I'm getting this:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
{"rules": [
{ "id_categoria": "3","categoria": "cat03" },{ "id_categoria": "2","categoria": "cat02" }
]
}</body>
</html>
Is it correct to find html, head, meta, and body tags in a JSON response?
By the way, I'm using this JSON parser: https://github.com/stig/json-framework/
And I think JSON response it also incorrect. Its XML equivalent is:
<?xml version="1.0" encoding="UTF-8"?>
<rules>
<id_categoria>3</id_categoria>
<categoria>cat03</categoria>
</rukes>
<rules>
<id_categoria>2</id_categoria>
<categoria>cat02</categoria>
</rules>
Is this XML correct?
I think a correct JSON response could be:
{
"data": {
"rules": [
{
"id_categoria": "3","categoria": "cat03"
},
{
"id_categoria": "2","categoria": "cat02"
}
]
}
}
Which its XML equivalent is:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<?xml version="1.0" encoding="UTF-8"?>
<rules>
<id_categoria>3</id_categoria>
<categoria>cat03</categoria>
</rukes>
<rules>
<id_categoria>2</id_categoria>
<categoria>cat02</categoria>
</rules>
</data>
I'm lost. Which is the correct format for JSON Response?
No, that's invalid JSON. The web service really needs to fix it - it'd be possible to parse the JSON out with some work, but it definitely shouldn't be designed to work this way.
The valid JSON is between the <body>
tags:
{
"rules": [
{
"id_categoria": "3",
"categoria": "cat03"
},
{
"id_categoria": "2",
"categoria": "cat02"
}
]
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.