Home > Net >  Convert json data to DataTbale or DataRow without loop
Convert json data to DataTbale or DataRow without loop

Time:01-12

Hi every one it is possible to convert json to debatable without doing loop

{
    "ipAddress":    "10.203.10.162",
    "portNo":   45462,
    "protocol": "HTTP",
    "macAddress":   "98:df:82:86:a4:27",
    "channelID":    1,
    "dateTime": "2023-01-11T14:57:24 04:00",
    "activePostCount":  1,
    "eventType":    "AccessControllerEvent",
    "eventState":   "active",
    "eventDescription": "Access Controller Event",
    "AccessControllerEvent":    {
        "deviceName":   "Access Controller",
        "majorEventType":   5,
        "subEventType": 75,
        "name": "ahmad",
        "cardReaderKind":   1,
        "cardReaderNo": 1,
        "verifyNo": 159,
        "employeeNoString": "1",
        "serialNo": 868,
        "userType": "normal",
        "currentVerifyMode":    "cardOrFaceOrFp",
        "currentEvent": false,
        "frontSerialNo":    867,
        "attendanceStatus": "breakOut",
        "label":    "Break Out",
        "statusValue":  0,
        "mask": "unknown",
        "helmet":   "unknown",
        "picturesNumber":   1,
        "purePwdVerifyEnable":  true
    }
}

I tried the following code and it return error.

DataTable dt = JsonConvert.DeserializeObject(json);

CodePudding user response:

You have to flatten your JObject and only after this you can deserialize it to DataTable

var jo=JObject.Parse(json);
jo.Merge(jo["AccessControllerEvent"]);
jo.Remove("AccessControllerEvent");

DataTable dt = new JArray(jo).ToObject<DataTable>();

CodePudding user response:

I suggest looking at uses of map: map documentation. Under the hood, this of course is doing a for loop of a kind.

Perhaps you are trying to avoid the 'traditional' for loop, for this i use :

Object.Keys(yourJsonObect).foreach((key) => {
    const someDataFromkey = yourJsonObect[key];
});

I hope that helps.

  • Related