0

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

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

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

上記のように、キーの前にあるjsonDictionaryはキーですが、より多くのキーが内部にあるキーが必要です。「forKeys:keys、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"}

配列キーにはText1、text2 ...が含まれ、配列オブジェクトにはUntitled、size ...が含まれますが、「textwriting」、「size」...がすべて一緒に含まれていないキー「text1」が必要です。

このようなものかもしれません:

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

助けてくれてありがとう

4

1 に答える 1

1

やりたいことを実現するには、ネストされたNSDictionariesの階層を作成する必要があります。

このようなもの:

// 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は次の構造になります。

別のNSDictionaryを保持するキー「メニュー」を持つNSDictionary。このディクショナリには、文字列を保持するキー「header」と、NSDictionariesのNSArrayを保持するキー「items」があります。

于 2012-05-06T16:23:02.590 に答える