Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var
- cLevel: array[1..100] of Integer;
- function TfrmUtama.IncLevel(ALevel: Integer): Integer;
- begin
- inc(ALevel);
- cLevel[ALevel] := 0;
- result := ALevel;
- end;
- function TfrmUtama.PrettyJSON(AJSON: TJSONObject; var level: Integer): String;
- const
- cSpasi = 4;
- var
- iii: Integer;
- jsp: TJSONPair;
- sss: String;
- res: String;
- cls: TClass;
- sps: String;
- cnt: Integer;
- jjj: Integer;
- jsa: TJSONObject;
- buk: String;
- begin
- res := '';
- for iii := 0 to AJSON.Count - 1 do
- begin
- jsp := AJSON.Pairs[iii];
- sss := jsp.JsonString.Value;
- cls := jsp.JsonValue.ClassType;
- sps := DupeString(' ', Level * cSPasi);
- cLevel[Level] := cLevel[Level] + 1;
- if cls = TJSONObject then
- begin
- if cLevel[Level] > 1 then
- res := res + ','#13#10 else
- res := res + #13#10;
- Level := incLevel(Level);
- jsa := TJSONObject(jsp.JsonValue);
- res := res + sps + '"' + sss + '" : {' + PrettyJSON(jsa, Level);
- res := res + #13#10 + sps + '}';
- dec(Level);
- end else
- if cls = TJSONArray then
- begin
- if cLevel[Level] > 1 then
- res := res + ','#13#10 else
- res := res + #13#10;
- res := res + sps + '"' + sss + '" : [';
- buk := sps;
- sps := sps + DupeString(' ', cSPasi);
- cnt := TJSONObject(jsp.JsonValue).Count;
- for jjj := 0 to cnt - 1 do
- begin
- Level := incLevel(Level);
- if jjj > 0 then
- res := res + ',';
- Level := incLevel(Level);
- jsa := TJSONObject(jsp.JsonValue.A[jjj]);
- res := res + #13#10 + sps + '{';
- res := res + sps + PrettyJSON(jsa, Level);
- res := res + #13#10;
- res := res + sps + '}';
- dec(Level);
- dec(Level);
- end;
- res := res + #13#10 + buk + ']';
- end else
- begin
- if cLevel[Level] > 1 then
- res := res + ','#13#10 else
- res := res + #13#10;
- res := res + format('%s"%s" : "%s"', [sps, sss, jsp.JsonValue.Value]);
- end;
- end;
- result := res;
- end;
- function TfrmUtama.PrettyFormat(AText: String): String;
- var
- sss: String;
- jso: TJSONObject;
- lev: Integer;
- begin
- sss := AText;
- jso := TJSONObject(TJSONObject.ParseJSONValue(sss));
- if jso <> nil then
- begin
- lev := 1;
- cLevel[1] := 0;
- sss := '{';
- sss := sss + PrettyJSON(jso, lev);
- sss := sss + #13#10'}';
- end;
- result := sss;
- end;
Advertisement