如何在表视图部分中显示此json格式

时间:2015-07-06 09:28:57

标签: json

如何在表格视图部分中显示此json格式

[
  {
    "success": "1",
    "message": "Data Listed Successfully",
    "events": [
      {
        "event_id": "3",
        "event_name": "New Years Dance Camp",
        "event_type": "New Years",
        "event_start_date": "2015-07-05",
        "event_start_time": "03:00 pm",
        "event_end_date": "2015-07-09",
        "event_end_time": "10:00 pm",
        "address": "anjali"
      },
      {
        "event_id": "4",
        "event_name": "Dance Meeting",
        "event_type": "meeting",
        "event_start_date": "2015-07-11",
        "event_start_time": "08:00 am",
        "event_end_date": "2015-07-15",
        "event_end_time": "07:00 pm",
        "address": "naroda"
      },
      {
        "event_id": "19",
        "event_name": "Dance India Dance",
        "event_type": "dance",
        "event_start_date": "2015-07-02",
        "event_start_time": "11:37 am",
        "event_end_date": "2015-07-02",
        "event_end_time": "02:35 pm",
        "address": "mumbai"
      },
      {
        "event_id": "20",
        "event_name": "Dance Plus",
        "event_type": "dance",
        "event_start_date": "2015-07-02",
        "event_start_time": "11:43 am",
        "event_end_date": "2015-07-02",
        "event_end_time": "11:42 am",
        "address": "Mumbai"
      },
      {
        "event_id": "21",
        "event_name": "Arijit Show",
        "event_type": "music",
        "event_start_date": "2015-07-02",
        "event_start_time": "02:25 pm",
        "event_end_date": "2015-07-02",
        "event_end_time": "02:28 pm",
        "address": "mumbai"
      }
    ],
    "shopping": [
      {
        "shopping_id": "3",
        "item_name": "boys dresses",
        "quantity": "15",
        "type": "single",
        "status": "",
        "deadline_date": "2015-07-05"
      },
      {
        "shopping_id": "8",
        "item_name": "Cap",
        "quantity": "2",
        "type": "Clothes",
        "status": "",
        "deadline_date": "2015-07-04"
      },
      {
        "shopping_id": "9",
        "item_name": "Hat",
        "quantity": "2",
        "type": "Clothes",
        "status": "pending",
        "deadline_date": "2015-07-04"
      }
    ],
    "weekly_schedule": [
      {
        "weekly_id": "23",
        "class": "Ballet/Jazz/Lyrical/Hip Hop",
        "address": "7606 Old Alexandria Ferry Road , Clinton, MD 20735",
        "start_time": "05:00 pm",
        "end_time": "08:30 pm",
        "days_in_week": "Sunday,",
        "dates_in_week": "05,12,19,26"
      },
      {
        "weekly_id": "24",
        "class": "Tap/Jazz/Lyrical/Hip Hop",
        "address": "7606 Old Alexandria Ferry Road , Clinton, MD 20735",
        "start_time": "06:00 pm",
        "end_time": "08:30 pm",
        "days_in_week": "Wednesday,",
        "dates_in_week": "01,08,15,22,29"
      },
      {
        "weekly_id": "25",
        "class": "Acro",
        "address": "4891 Tesla Drive #J, Bowie, MD 20815-4317",
        "start_time": "08:15 pm",
        "end_time": "09:00 pm",
        "days_in_week": "Wednesday,",
        "dates_in_week": "01,08,15,22,29"
      },
      {
        "weekly_id": "26",
        "class": "hippop",
        "address": "satellite",
        "start_time": "05:30 am",
        "end_time": "05:30 pm",
        "days_in_week": "Sunday",
        "dates_in_week": "05,12,19,26"
      },
      {
        "weekly_id": "27",
        "class": "pop ",
        "address": "C.G Road",
        "start_time": "04:30 am",
        "end_time": "04:30 pm",
        "days_in_week": "Sunday",
        "dates_in_week": "05,12,19,26"
      }
    ],
    "competition": [
      {
        "competition_id": "12",
        "event_name": "World dance",
        "event_type": "weekly",
        "start_date": "2015-07-04",
        "start_time": "11:00 am",
        "end_date": "2015-07-07",
        "end_time": "12:00 pm",
        "competition_date": "2015-07-04",
        "address": "Rock Regency"
      },
      {
        "competition_id": "13",
        "event_name": "Just Dancer",
        "event_type": "Competition",
        "start_date": "2015-07-10",
        "start_time": "12:00 pm",
        "end_date": "2015-07-15",
        "end_time": "05:00 pm",
        "competition_date": "2015-07-10",
        "address": "Holiday Blue"
      },
      {
        "competition_id": "23",
        "event_name": "monthly dance",
        "event_type": "year",
        "start_date": "2015-07-10",
        "start_time": "06:15 am",
        "end_date": "2015-07-15",
        "end_time": "07:15 pm",
        "competition_date": "2015-07-10",
        "address": "abu"
      },
      {
        "competition_id": "22",
        "event_name": "Dance India Dance",
        "event_type": "Dance",
        "start_date": "2015-07-03",
        "start_time": "01:26 pm",
        "end_date": "2015-07-03",
        "end_time": "01:27 pm",
        "competition_date": "2015-07-03",
        "address": "Mumbai"
      }
    ],
    "carpool": [
      {
        "carpool_id": "8",
        "driver_name": "Rahul",
        "source": "Mumbai",
        "destination": "",
        "carpool_date": "2015-07-03",
        "start_time": "01:26 pm",
        "end_time": "01:26 pm"
      },
      {
        "carpool_id": "11",
        "driver_name": "sonal",
        "source": "Mumbai",
        "destination": "",
        "carpool_date": "2015-07-04",
        "start_time": "08:16 am",
        "end_time": "08:16 am"
      }
    ]
  }
]

2 个答案:

答案 0 :(得分:0)

您应该使用parseJSON来检索数据。

用于解析JSON数据并将其存储在表中的代码:

function tableCreate(){

var data= {};
data= $.parseJSON('[{"name":"aaaa","description":"ababab"},{"name":"bbbb","description":"bcbcbc"},{"name":"cccc","description":"dcdcdc"}]');
var tableData = "";
tableData +="<table><th>Name</th><th>Description</th>";

$.each(data,
            function(key, val) {
                console.log(tableData);
            tableData += "<tr><td>"+val.name+"</td>" ;
            tableData += "<td>" + val.description + "</td></tr>";
                console.log(tableData);
});
tableData +="</table>";
$('body').append(tableData);
}

查看我的示例jsfiddle链接:https://jsfiddle.net/guqs9dv2/1/

答案 1 :(得分:0)

您必须重新格式化数据以在tableview中明智地显示数据,我认为以下代码段将适合您。

NSArray *mainDataArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
NSMutableDictionary *mainDataDict = [NSMutableDictionary dictionaryWithDictionary:[mainDataArray firstObject]];
NSArray *allKeys = [mainDataDict allKeys];

    if ([mainDataDict[@"success"] boolValue]==YES) {
        NSMutableArray *tableArray = [NSMutableArray array]; // Use your preferred variable here to use in TableView
        for (NSString *aKey in allKeys) {
            if ([aKey isEqualToString:@"success"] || [aKey isEqualToString:@"message"]) {
                continue;
            }
            NSMutableDictionary *newData = [NSMutableDictionary dictionary];
            newData[@"category"] = aKey;
            newData[@"items"] = mainDataDict[aKey];
            [tableArray addObject:newData];
        }
    }
    else{
        // handle error
    }

根据上面的代码段,您可以使用"category"键作为标题标题和"items"键,以获取category的相应项目。