在iOS中进行JSON解析时将重复的值合并到单个数组中

时间:2018-11-04 09:42:46

标签: objective-c arrays json merge

我想解析json并将其保存在本地数组中,以便可以在应用程序中像excell一样显示数据。 json是多余的。我上传了一张图片供您参考。 这是我的json

    [
  {
    "date": "2018-10-21",
    "Status": "Confirmed",
    "Orders": "1"
  },
  {
    "date": "2018-10-22",
    "Status": "Confirmed",
    "Orders": "1"
  },
  {
    "date": "2018-10-22",
    "Status": "Delivered",
    "Orders": "2"
  },
  {
    "date": "2018-10-22",
    "Status": "Cancelled",
    "Orders": "1"
  },
  {
    "date": "2018-10-22",
    "Status": "Rejected",
    "Orders": "2"
  },
  {
    "date": "2018-10-22",
    "Status": "Cancelled",
    "Orders": "1"
  },
  {
    "date": "2018-10-22",
    "Status": "Cancelled",
    "Orders": "1"
  },
  {
    "date": "2018-10-23",
    "Status": "Cancelled",
    "Orders": "2"
  },
  {
    "date": "2018-10-23",
    "Status": "Processing",
    "Orders": "3"
  },
  {
    "date": "2018-10-24",
    "Status": "Cancelled",
    "Orders": "1"
  },
  {
    "date": "2018-10-25",
    "Status": "Delivered",
    "Orders": "1"
  },
  {
    "date": "2018-10-25",
    "Status": "Processing",
    "Orders": "2"
  },
  {
    "date": "2018-10-26",
    "Status": "Delivered",
    "Orders": "1"
  },
  {
    "date": "2018-10-27",
    "Status": "Processing",
    "Orders": "3"
  },
  {
    "date": "2018-10-27",
    "Status": "Processing",
    "Orders": "1"
  },
  {
    "date": "2018-10-27",
    "Status": "Processing",
    "Orders": "2"
  },
  {
    "date": "2018-10-28",
    "Status": "Confirmed",
    "Orders": "1"
  },
  {
    "date": "2018-10-28",
    "Status": "Delivered",
    "Orders": "3"
  },
  {
    "date": "2018-10-29",
    "Status": "Delivered",
    "Orders": "5"
  },
  {
    "date": "2018-10-29",
    "Status": "Processing",
    "Orders": "2"
  },
  {
    "date": "2018-10-29",
    "Status": "Rejected",
    "Orders": "1"
  },
  {
    "date": "2018-10-29",
    "Status": "Processing",
    "Orders": "1"
  },
  {
    "date": "2018-10-31",
    "Status": "Confirmed",
    "Orders": "2"
  },
  {
    "date": "2018-10-31",
    "Status": "Processing",
    "Orders": "2"
  },
  {
    "date": "2018-10-31",
    "Status": "Processing",
    "Orders": "1"
  },
  {
    "date": "2018-10-31",
    "Status": "Processing",
    "Orders": "1"
  },
  {
    "date": "2018-11-01",
    "Status": "Confirmed",
    "Orders": "1"
  },
  {
    "date": "2018-11-01",
    "Status": "Processing",
    "Orders": "1"
  },
  {
    "date": "2018-11-01",
    "Status": "Processing",
    "Orders": "2"
  },
  {
    "date": "2018-11-01",
    "Status": "Processing",
    "Orders": "1"
  },
  {
    "date": "2018-11-02",
    "Status": "Cancelled",
    "Orders": "1"
  },
  {
    "date": "2018-11-03",
    "Status": "Processing",
    "Orders": "4"
  },
  {
    "date": "2018-11-03",
    "Status": "Cancelled",
    "Orders": "1"
  },
  {
    "date": "2018-11-04",
    "Status": "Processing",
    "Orders": "1"
  }
]

解析之后,我想将同一日期的数据保存在一起。例如,对于特定日期(例如2018-10-20),我想将所有信息保留在实体类中,以便可以在表中填充信息。

enter image description here

下面提供了我的解析代码

    -(void)parseLocalJson
{
    resultsArray=[[NSMutableArray alloc]init];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Results" ofType:@"json"];
    NSString *myJSON = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
    NSArray *jsonArr = [NSJSONSerialization JSONObjectWithData:[myJSON dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];

    for(int r =0;r<[jsonArr count];)
    {
        id val=[jsonArr objectAtIndex:r];
        Results* prevResult;
        //Results* aResult;
        Results* aResult=[[Results alloc]init];
        aResult.date=[val valueForKey:@"date"];


         if(![aResult.date isEqualToString:prevResult.date])
        {
             //Results* aResult=[[Results alloc]init];
            NSString* status=[val valueForKey:@"Status"];
            NSString* orders=[val valueForKey:@"Orders"];
            if([status isEqualToString:@"Confirmed"])
            {
                aResult.orderStatus_Confirmed=orders;
            }
            else if([status isEqualToString:@"Delivered"])
            {
                aResult.orderStatus_Delivered=orders;
            }
            else if([status isEqualToString:@"Cancelled"])
            {
                aResult.orderStatus_Cancelled=orders;

            }
            else if([status isEqualToString:@"Rejected"])
            {
                aResult.orderStatus_Rejected=orders;
            }
            else if([status isEqualToString:@"Pending"])
            {
                aResult.orderStatus_Pending=orders;
            }
            [resultsArray addObject:aResult];
            r++;
        }
        else
        {
            r--;
            NSString* status=[val valueForKey:@"Status"];
            NSString* orders=[val valueForKey:@"Orders"];
            if([status isEqualToString:@"Confirmed"])
            {
                prevResult.orderStatus_Confirmed=orders;
            }
            else if([status isEqualToString:@"Delivered"])
            {
                prevResult.orderStatus_Delivered=orders;
            }
            else if([status isEqualToString:@"Cancelled"])
            {
                prevResult.orderStatus_Cancelled=orders;

            }
            else if([status isEqualToString:@"Rejected"])
            {
                prevResult.orderStatus_Rejected=orders;
            }
            else if([status isEqualToString:@"Pending"])
            {
                prevResult.orderStatus_Pending=orders;
            }

            //[resultsArray addObject:prevResult];

        }
        prevResult=aResult;




    }





    // Parse the string into JSON
    //NSDictionary *json = [myJSON JSONValue];
}

我想根据提供的图像将数据保留在数组中。请帮帮我。提前致谢。

0 个答案:

没有答案