如何在使用 JSON 响应时从数组中获取包含对象和数组中已有对象的值?

how to gat a value from an array which contain in an object and object already in an array while using json response?

提问人:singh 提问时间:3/23/2017 最后编辑:Tamas Revsingh 更新时间:3/24/2017 访问量:178

问:

[
{
  "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”又是一个位于对象下的数组,现在我该如何获取product_qty ]1 的元素值

Objective-C iPhone iOS6

评论

0赞 iCoderz Developers 3/23/2017
让我知道它是否有效。

答:

-1赞 CodeChanger 3/23/2017 #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.

希望这会有所帮助。

-1赞 iCoderz Developers 3/23/2017 #2
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"]);

}
}
0赞 saurabh rathod 3/24/2017 #3

刚刚用过这个 if([数组包含对象字典]) { } 还 {already in your arraynot in your array }