你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> nsdictionary獲得指定key值

nsdictionary獲得指定key值

編輯:IOS開發綜合

問題描述:

在nsdictionary中保存json解析的結果:

 

[plain]

 "statusCode":"200", 
"body":[ 
  { 
     "status":"success", 
     "remarks":null 
  } 
], 
 
"data":[ 
     "abcd":[ 
        { 
           "category":"a", 
           "title":"b", 
           "id":"24" 
        }, 
        { 
           "category":"c", 
           "title":"crd", 
           "id":"65" 
        }, 
        { 
           "category":"ds", 
           "title":"sd", 
           "id":"18" 
        } 
 
     ] 
  }, 
  { 
     "efgh":[ 
        { 
           "category":"ds", 
           "title":"sd", 
           "id":"18" 
        }, 
        { 
           "category":"sd", 
           "title":"sd", 
           "id":"1" 
        } 
 
                             ] 
  }, 
  { 
     "ijkl":[ 
        { 
           "category":"ds", 
           "title":"sd", 
           "id":"18" 
        }, 
        { 
           "category":"sd", 
           "title":"sd", 
           "id":"1" 
        } 
 
     ] 
  } 
 ] 

{
 "statusCode":"200",
"body":[
  {
     "status":"success",
     "remarks":null
  }
],

"data":[
     "abcd":[
        {
           "category":"a",
           "title":"b",
           "id":"24"
        },
        {
           "category":"c",
           "title":"crd",
           "id":"65"
        },
        {
           "category":"ds",
           "title":"sd",
           "id":"18"
        }

     ]
  },
  {
     "efgh":[
        {
           "category":"ds",
           "title":"sd",
           "id":"18"
        },
        {
           "category":"sd",
           "title":"sd",
           "id":"1"
        }

                             ]
  },
  {
     "ijkl":[
        {
           "category":"ds",
           "title":"sd",
           "id":"18"
        },
        {
           "category":"sd",
           "title":"sd",
           "id":"1"
        }

     ]
  }
 ]
}
關鍵字@data的數據可以保存在數組中:

 

[plain]
NSMutableArray *getdata=[[NSMutableArray alloc]init]; 
getcat=[results objectForKey:@"data"]; 

NSMutableArray *getdata=[[NSMutableArray alloc]init];
getcat=[results objectForKey:@"data"];
下面應該怎麼設置在第一個索引中的values(category, title, id)。比如'abcd'?

謝謝

解決方案:
getdata 裡面應該是 3個NSDictionary 分別是


[plain]

       "category":"a", 
       "title":"b", 
       "id":"24" 
    }, 
    { 
       "category":"c", 
       "title":"crd", 
       "id":"65" 
    }, 
    { 
       "category":"ds", 
       "title":"sd", 
       "id":"18" 
    } 

{
       "category":"a",
       "title":"b",
       "id":"24"
    },
    {
       "category":"c",
       "title":"crd",
       "id":"65"
    },
    {
       "category":"ds",
       "title":"sd",
       "id":"18"
    }
你要設置在第一個索引中的values


[plain]
NSDictionary *firstDic = [getdata objectAtIndex:0]; 
 [firstDic setValue:@"new category" forKey:@"category"]; 
  [firstDic setValue:@"new title" forKey:@"title"]; 
  [firstDic setValue:@"new id" forKey:@"id"]; 

NSDictionary *firstDic = [getdata objectAtIndex:0];
 [firstDic setValue:@"new category" forKey:@"category"];
  [firstDic setValue:@"new title" forKey:@"title"];
  [firstDic setValue:@"new id" forKey:@"id"];

 

 

 

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved