如何在使用 json 响应时从包含在对象中的数组和已经在数组中的对象中获取值?
how to gat a value from an array which contain in an object and object already in an array while using json response?
[
{
"result": 1,
"max_qty": "10",
"msg": [
{
"product_id": "39",
"product_name": "Cabbage",
"product_price": "18",
"product_image": "product/39.jpg",
"product_description": "Fresh Cabbage",
"product_category": "1",
"product_qty": [
{
"quantity_id": "3",
"quantity_name": "500 gm",
"value": "500",
"quantity_status": "1"
},
{
"quantity_id": "4",
"quantity_name": "1 Kg",
"value": "1000",
"quantity_status": "1"
}
在这个 "msg" 中,它是一个数组,包含对象形式的值,但 "product_qty" 又是一个对象下的数组,现在我如何获取 [=17 的元素值=]]1
您需要像这样获取键值:
NSDictionary *dictProducts = array[0]; // From Main Array.
NSArray *allProduct = dictProducts[@"msg"]; // Form Dictionary.
NSDictionary *firstProduct = allProduct[0];
NSLog(@"Product Qty : %@", firstProduct[@"product_qty"]); // Here you can get product_qty array.
希望这会有所帮助。
Updated Code:
NSDictionary *mainDic = [arrResponse objectAtIndex:0];
NSArray *arrMsg = [mainDic valueForKey:@"msg"];
for (NSDictionary *dicSub in arrMsg) {
NSArray *arrProdQuality = [dicSub valueForKey:@"product_qty"];
for (NSDictionary *dicQuality in arrProdQuality) {
NSLog(@"quantity_name %@",[dicQuality valueForKey:@"quantity_name"]);
}
}
刚用过这个
if([数组包含对象字典])
{
already in your array
}
别的
{
not in your array
}
[
{
"result": 1,
"max_qty": "10",
"msg": [
{
"product_id": "39",
"product_name": "Cabbage",
"product_price": "18",
"product_image": "product/39.jpg",
"product_description": "Fresh Cabbage",
"product_category": "1",
"product_qty": [
{
"quantity_id": "3",
"quantity_name": "500 gm",
"value": "500",
"quantity_status": "1"
},
{
"quantity_id": "4",
"quantity_name": "1 Kg",
"value": "1000",
"quantity_status": "1"
}
在这个 "msg" 中,它是一个数组,包含对象形式的值,但 "product_qty" 又是一个对象下的数组,现在我如何获取 [=17 的元素值=]]1
您需要像这样获取键值:
NSDictionary *dictProducts = array[0]; // From Main Array.
NSArray *allProduct = dictProducts[@"msg"]; // Form Dictionary.
NSDictionary *firstProduct = allProduct[0];
NSLog(@"Product Qty : %@", firstProduct[@"product_qty"]); // Here you can get product_qty array.
希望这会有所帮助。
Updated Code:
NSDictionary *mainDic = [arrResponse objectAtIndex:0];
NSArray *arrMsg = [mainDic valueForKey:@"msg"];
for (NSDictionary *dicSub in arrMsg) {
NSArray *arrProdQuality = [dicSub valueForKey:@"product_qty"];
for (NSDictionary *dicQuality in arrProdQuality) {
NSLog(@"quantity_name %@",[dicQuality valueForKey:@"quantity_name"]);
}
}
刚用过这个
if([数组包含对象字典])
{
already in your array
}
别的
{
not in your array
}