更改 NSDictionary 中的 json 格式 (Objective C)

Change json format in NSDictionary (Objective C)

我是 ios 编程新手。我应该将数据应用于图表。但是我使用的框架(ShinobiControls)只接受特定格式的json。所以我必须将 json 数据格式更改为适当的。我有 NSDictionary,其中包含 json,如下所示:

"data": [
    "01.01.2015",
    "01.01.2015",
    "01.01.2015",
    "01.01.2015"]
"close": [
    [
      1,
      1,
      1,
      1]

现在我应该像这样更改 json 的格式:

[
  {
    "date": "01.01.2015",
    "close": 1
  },
  {
    "date": "01.01.2015",
    "close": 1
  },
  {
    "date": "01.01.2015",
    "close": 1
  },
  {
    "date": "01.01.2015",
    "close": 1
  }
]

我对将 NSDictionary 转换为 NSArray 进行了一些操作,但没有得到任何结果。我该怎么做?你有什么想法?谢谢。

所以如果我没看错你的问题,你有一个包含 2 个数组的字典,你想将它转换成一个包含字典的数组,假设字典中数组的数量相等,你可以执行以下操作

//This is the first array in your dictionary 
NSArray * dataArr = [data objectForKey:@"data"] ;
//This the second array in your dictionary 
NSArray * closeArr = [data objectForKey:@"close"] ;

NSUInteger  dataCount = [dataArr count] ;
NSUInteger  closeCount = [closeArr count] ;

//This will be your result array
NSMutableArray * newData = [NSMutableArray new] ;

//The loop condition checks that the current index is less than both the arrays 
for(int i = 0 ; i<dataCount && i<closeCount ; i++)
{
    NSMutableDictionary * temp = [NSMutableDictionary new] ;
    NSString * dataString = [dataArr objectAtIndex:i];
    NSString * closeString = [closeArr objectAtIndex:i];
    [temp setObject:dataString forKey:@"date"];
    [temp setObject:closeString forKey:@"close"] ;

    [newData addObject:temp];
}
NSArray *Arr = [[NSArray alloc] initWithObjects:@"01.01.2015",@"01.01.2015",@"01.01.2015",@"02.01.2015", nil];
NSArray *Arr1 = [[NSArray alloc] initWithObjects:@"1",@"1",@"1",@"1", nil];
NSDictionary *Dic = [[NSDictionary alloc] initWithObjectsAndKeys:Arr,@"data",Arr1,@"close", nil];
NSLog(@"%@",Dic);
NSMutableArray *ArrM = [[NSMutableArray alloc] init];
for ( int  i = 0; i<Arr.count; i++) {
    NSDictionary *Dic = [[NSDictionary alloc] initWithObjectsAndKeys:Arr[i],@"data",Arr1[i],@"close", nil];
    [ArrM addObject:Dic];
}
NSLog(@"%@",ArrM);
NSError * err;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:ArrM options:0 error:&err];
NSString * myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"%@",myString);