在我的应用程序中,我使用了Json。这是我的应用程序中的JSON响应
This is my Response:
[
{
"response": "Success",
"errorMsg": "",
"userId": "1",
"userCompany": "xxxyy",
"userName": "sham",
"userAddress": "chennai",
"userCity": "xxxxx",
"userMobile": "xxxx",
"userEmail": "xxx"
},
{
"response": "Success",
"errorMsg": "",
"productImage": "http://www.iii.jpg",
"productDescription": "Loaded box on pallets - Bart's package",
"productCost": "10",
"productBoxWeight": "10.0"
},
{
"response": "Success",
"errorMsg": "",
"transportCost": "1.4",
"transportCountry": "Colombia",
"transportPort": "Havana"
},
{
"response": "Success",
"errorMsg": "",
"transportCost": "0.7",
"transportCountry": "Brazil",
"transportPort": "Santos"
},
{
"response": "Success",
"errorMsg": "",
"transportCost": "0.9",
"transportCountry": "South Africa",
"transportPort": "Durban"
},
{
"response": "Success",
"errorMsg": "",
"transportCost": "0.9",
"transportCountry": "Chili",
"transportPort": "San Antonio"
},
{
"response": "Success",
"errorMsg": "",
"transportCost": "2.7",
"transportCountry": "Australia",
"transportPort": "Maersk"
},
{
"response": "Success",
"errorMsg": "",
"transportCost": "1",
"transportCountry": "Marocco",
"transportPort": "Casablanca"
},
{
"response": "Success",
"errorMsg": "",
"transportCost": "1",
"transportCountry": "Kuwait",
"transportPort": "Shuwaikh"
},
{
"response": "Success",
"errorMsg": "",
"transportCost": "1",
"transportCountry": "Jordan",
"transportPort": "Aqaba"
},
{
"response": "Success",
"errorMsg": "",
"transportCost": "0.8",
"transportCountry": "Saoudi Arabia",
"transportPort": "Jeddah"
},
{
"response": "Success",
"errorMsg": "",
"transportCost": "0.8",
"transportCountry": "Malta",
"transportPort": "Maraxklokk"
},
{
"response": "Success",
"errorMsg": "",
"transportCost": "0.9",
"transportCountry": "Mexico",
"transportPort": "Veracruz"
},
{
"response": "Success",
"errorMsg": "",
"transportCost": "1.2",
"transportCountry": "Thailand",
"transportPort": "Bangkok"
},
{
"response": "Success",
"errorMsg": "",
"transportCost": "1",
"transportCountry": "Thailand",
"transportPort": "havana"
}
]
我怎样才能在一个阵列中检索前两个集合,在另一个阵列中检索其他集合...我是新手,请帮我修复它...
答案 0 :(得分:0)
JSON没有按任何顺序定义键或值(包括JSON数组中的值)。因此,您必须在解析后自己对项目进行排序。
为了解析JSON,您可以使用any JSON parser available(我更喜欢SBJson)。 通常,解析器提供了将JSON转换为NSDictionary的可能性,因此您可以轻松处理其内容。
答案 1 :(得分:0)
为了解析您的JSON并获取Objective-C对象,请使用以下代码(其中data
是您刚刚获得的JSON):
NSError *e = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&e];
之后,您将拥有一个包含多个词典(NSArray
)的NSDictionary
。要获得第一个,请执行:
if (!jsonArray) {
NSLog(@"Error parsing JSON: %@", e);
} else {
// get the first dictionary
NSDictionary *dict = [jsonArray objectAtIndex:0];
}