代码改变世界

JSON代码示例

2018-08-14 09:06  圣二十一郎  阅读(612)  评论(0编辑  收藏  举报

生成JSON:

var
  json: ISuperObject;
  i: Integer;
  jsonList, jsonRow: ISuperObject;
begin
  json := SO();
  jsonList := SO('[]');
  try
    try
      for I := 0 to EventList.Count - 1 do
      begin
        jsonRow := SO('{}');
        jsonRow.S['strGUID'] := EventList[I].strEventID;
        jsonRow.S['strCaption'] := EventList[i].EventProperty.GetEventTypeName; 
        jsonRow.I['nTriggerLC'] := EventList[i].nTriggerGLB;
        jsonRow.I['nSectionBeginLC'] := EventList[i].nSectionBeginGLB;
        jsonRoW.I['nSectionEndLC'] := EventList[i].nSectionEndGLB;
        jsonList.AsArray.Add(jsonRow);    
      end;
      
      json.I['cmdtype'] := ord(esuctNew);
      json.O['EventList'] := jsonList;
      m_IdUDPClient.Send(json.AsString);

    except
      on e: Exception do
      begin
        AddLog('新的演练命令错误:' + e.Message);
      end;
    end;
  finally
    json := nil;
  end;

解析JSON:

var
  dd: TSuperArray;
  i: Integer;
  Eventobj: TQuestionEvent;
  json: ISuperObject;
begin
  json := SO(strMsg);
  try
    cmdType :=json.I['cmdtype']; 
    dd := json.O['EventList'].AsArray;
    for I := 0 to dd.Length - 1 do
    begin
      EventObj := TQuestionEvent.Create; 
      Eventobj.strEventID := dd[i].S['strGUID'];
      Eventobj.strOptHint := dd[i].S['strCaption'];
      Eventobj.nTriggerGLB := dd[i].I['nTriggerLC'];
      Eventobj.nSectionBeginGLB := dd[i].I['nSectionBeginLC']; 
      Eventobj.nSectionEndGLB := dd[i].I['nSectionEndLC'];
    
      EventList.Add(Eventobj);
    end;
  finally
      
  end;