2016 m. gruodžio 15 d., ketvirtadienis

Decode JSON with Embarcadero Delphi XE2 example

interface
uses  ....,DBXJSON;

 private
    FJSONObject: TJSONObject; 
...
var
  S: TStringList;
  JsonArray, JsonArray1: TJSONArray;
  _messagetype,_nameStation,_installedCapacity,tramp1:string;
  i,j:integer;
begin
  S := TStringList.Create;
  S.LoadFromFile(getcurrentdir + '\' + ... + '.json');
  FJSONObject := TJSONObject.ParseJSONValue(S.Text) as TJSONObject;
  JsonArray := FJSONObject.Get('items').JsonValue as TJSONArray;
  for I := 0 to JsonArray.Size - 1 do
    begin
      _messagetype := trim((JsonArray.Get(i) as TJSONObject).Get('messageType').JsonValue.Value);
      ....// next values
      tramp1 := (JsonArray.Get(i) as TJSONObject).Get('productionUnits').JsonValue.ClassName;//check type
      if tramp1 = 'TJSONArray' then //if array and NOT empty; one question - two answers:)
        begin
          JsonArray1 := (JsonArray.Get(i) as TJSONObject).Get('productionUnits').JsonValue as TJSONArray;//if array then read to array
        for j := 0 to JsonArray1.Size - 1 do
        begin
          _nameStation := (JsonArray1.Get(j) as TJSONObject).Get('name').JsonValue.Value;
          _installedCapacity := (JsonArray1.Get(j) as TJSONObject).Get('installedCapacity').JsonValue.Value;
          ...
        end;
    end;
end;
//------------------------------
//JSON file example from new 2017 NordPoolSpot UMM system:
//------------------------------
{"items":
[
  {
   "messageType":1,  //read in example
   "unavailabilityType":2,
   "reasonCode":2,
   "eventStart":null,
   "eventStop":null,
   "unavailabilityReason":"TEST",
   "remarks":null,
   "productionUnits":[], //this is array! read in example, check type, load to TJsonArray
   "generationUnits":   
       [                 // next array, do as with "productionUnits", read "productionUnitName",...
          {
           "productionUnitName":"Power Plant x",
           "productionUnitEic":"21W1123600241XL3",
           "productionUnitInstalledCapacity":1955,
           "areaEic":"11XYZ-1001A0008Q",
           "areaName":"XT","fuelType":4,
           "name":"G-10",
           "eic":"41WLE-------TG8I",
           "installedCapacity":300,
           "powerFeedIn":null,
           "timePeriods":[
                          {
                           "unavailableCapacity":300,
                           "availableCapacity":0,
                           "eventStart":"2016-12-10T03:10:00Z",
                           "eventStop":"2016-12-16T05:10:00Z"
                          },
                          {
                           "unavailableCapacity":150,
                           "availableCapacity":150,
                           "eventStart":"2016-12-16T05:10:00Z",
                           "eventStop":"2016-12-17T05:10:00Z"
                          }
                         ]
          }
       ],
   "consumptionUnits":null,
   "transmissionUnits":null,
   "otherUnits":null,"assets":[],
   "marketParticipants":
       [
          {"name":"\"CocaCola Plant group\", AB","code":"A0001293A.LT"}
       ],
   "eventStatus":1,
   "cancellationReason":null,
   "messageId":"492bcb75-6175-40e9-9496-104e1a4f246b",
   "version":2,
   "publicationDate":"2016-12-10T12:41:30.480019Z",
   "publisherId":"NC0102",
   "publisherName":"\"CocaCola Plant group\", AB"
},
],"total":1}
P.S.: there is no "wraps" in original file

Komentarų nėra: