2012-05-06 3 views
0

jsonファイルに複数のキーを追加するにはどうすればよいですか?JSON iPhoneにさらにキーを挿入する

NSArray *keys = [NSArray arrayWithArray:allkeys]; 
NSArray *objects = [NSArray arrayWithArray:allobjects]; 

NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 

上記のように、キーの前のjsonDictionaryはキーですが、内部にキーが多いキーが必要です。私は「forKeysを:キー、keys2」試してみました(keys2別の配列が、動作しません。今、これが結果です:。

{"text2":"Untitled {{400, 100}, {200, 100}} <UICFFont: 0x2c3960> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12px","text3":"Untitled {{400, 100}, {200, 100}} <UICFFont: 0x2c3960> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12px","text1":"Untitled {{400, 100}, {200, 100}} <UICFFont: 0x2c3960> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size:  12px"} 

配列のキーは、テキスト1、テキスト2が含まれています...と配列オブジェクトは、無題、サイズが含まれています...しかし、私はすべて一緒に...内部の「テキスト書き込み」、「サイズ」とキー「テキスト1」にないたい

はこのようなものである場合があります。

{ 
"menu": { 
    "header": "xProgress SVG Viewer", 
    "items": [ 
     { 
      "id": "Open" 
     }, 

事前に感謝ヘルプ

答えて

1

ネストしたNSDictionariesの階層を作成して、何をしたいのかを達成する必要があります。このような

何かが:別のNSDictionaryのを保持しているキー "メニュー"、と

A NSDictionaryの:

// the following objecst are just dummies, fill this with your own objects 
NSArray *objectArray = [NSArray arrayWithOjects:obj1,obj2,obj3,nil]; 
NSArray *keyArray = [NSArray arrayWithOjects:@"key1",@"key2,@"key3",nil]; 

NSDictionary *subDict = [NSDictionary dictionaryWithObjects:objectArray forKeys:keyArray]; 

// now create the main dictionary, which will hold your sub dictionary for one key 
objectArray = [NSArray arrayWithObjects:,subDict,someObject,@"just a string"]; 
keyArray = [NSArray arrayWithObjects:@"first key",@"second key",@"third key"]; 

NSDictionary *mainDict = [NSDictionary dictionaryWithObjects:objectArray forKeys:keyArray]; 

あなたの例JSONは、次のような構造を持っているでしょう。この辞書には、文字列を保持するキー "header"とNSDictionariesのNSArrayを保持するキー "items"があります。

関連する問題