151 lines
3.8 KiB
ObjectPascal
151 lines
3.8 KiB
ObjectPascal
![]() |
{<EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD> JSON <EFBFBD><EFBFBD>Ԫ
|
|||
|
}
|
|||
|
unit uFomat_JSON;
|
|||
|
|
|||
|
interface
|
|||
|
|
|||
|
uses
|
|||
|
system.JSON, System.SysUtils, System.Variants, System.Classes;
|
|||
|
|
|||
|
const
|
|||
|
Level_indent = 2;
|
|||
|
|
|||
|
|
|||
|
//<2F><>ʽ<EFBFBD><CABD> JSON <20>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
// JSONStr : <20><>ʾ<EFBFBD><CABE>Ҫ<EFBFBD><D2AA>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD> JSON<4F>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
// lv : <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>㼶
|
|||
|
function JSON_Format(JSONStr: string; lv: Word = 0): string;
|
|||
|
|
|||
|
|
|||
|
//<2F><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD>
|
|||
|
function JSON_Fromat_Array(ja: TJSONArray; lv: word = 0): string;
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>indent
|
|||
|
function LI(lv: Word): string;
|
|||
|
|
|||
|
implementation
|
|||
|
|
|||
|
//<2F><>ʽ<EFBFBD><CABD> JSON <20>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
// JSONStr : <20><>ʾ<EFBFBD><CABE>Ҫ<EFBFBD><D2AA>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD> JSON<4F>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
// Left_Space : <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>ͳһ<CDB3><D2BB><EFBFBD><EFBFBD><EFBFBD>ٸ<EFBFBD><D9B8>ո<EFBFBD>
|
|||
|
// Level_index <20><><EFBFBD><EFBFBD>ʾÿ<CABE><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ո<EFBFBD>
|
|||
|
function JSON_Format(JSONStr: string; lv: Word = 0): string;
|
|||
|
var
|
|||
|
S: string;
|
|||
|
jo: TJSONObject;
|
|||
|
jo1: TJSONObject;
|
|||
|
jp: TJSONPair;
|
|||
|
js: TJSONString;
|
|||
|
jn: TJSONNumber;
|
|||
|
jb: TJSONBool;
|
|||
|
ja: TJSONArray;
|
|||
|
jpe: TJSONObject.TEnumerator;
|
|||
|
begin
|
|||
|
//1. <20><><EFBFBD><EFBFBD> JSONStr
|
|||
|
jo := TJSONObject.ParseJSONValue(JSONStr) as TJSONObject;
|
|||
|
if jo = nil then
|
|||
|
Exit(JSONStr);
|
|||
|
|
|||
|
Result := '{' + #13#10;
|
|||
|
try
|
|||
|
jpe := jo.GetEnumerator;
|
|||
|
if jpe = nil then
|
|||
|
Exit(JSONStr);
|
|||
|
|
|||
|
while jpe.MoveNext do
|
|||
|
begin
|
|||
|
jp := jpe.Current;
|
|||
|
if jp.JsonValue.TryGetValue(jo1) then
|
|||
|
begin
|
|||
|
Result := Result + LI(lv + 1) + '"' + jp.JsonString.Value + '":'#13#10;
|
|||
|
Result := Result + LI(lv + 1) + JSON_Format(jo1.ToJSON, lv + 2);
|
|||
|
Continue;
|
|||
|
end
|
|||
|
else if jp.JsonValue.TryGetValue(jn) then //<2F><><EFBFBD><EFBFBD>
|
|||
|
Result := Result + LI(lv + 1) + '"' + jp.JsonString.Value + '": ' + jn.Value + ','#13#10
|
|||
|
else if jp.JsonValue.TryGetValue(jb) then //Boolean
|
|||
|
Result := Result + LI(lv + 1) + '"' + jp.JsonString.Value + '": ' + jb.Value + ','#13#10
|
|||
|
else if jp.JsonValue.TryGetValue(ja) then //<2F>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
begin
|
|||
|
Result := Result + LI(lv + 1) + '"' + jp.JsonString.Value + '": '#13#10;
|
|||
|
Result := Result + LI(lv + 1) + JSON_Fromat_Array(ja, lv + 1);
|
|||
|
end
|
|||
|
else if jp.JsonValue.TryGetValue(js) then //<2F>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
Result := Result + LI(lv + 1) + '"' + jp.JsonString.Value + '": "' + js.Value + '",'#13#10
|
|||
|
end;
|
|||
|
//ȥ<><C8A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>е<EFBFBD> ,
|
|||
|
S := Result.Substring(Length(Result) - 3, 3);
|
|||
|
if S = ','#13#10 then
|
|||
|
Result := Result.Substring(0, Length(Result) - 3) + #13#10;
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> }
|
|||
|
if lv > 0 then
|
|||
|
begin
|
|||
|
lv := lv - 1;
|
|||
|
Result := Result + LI(lv) + '},'#13#10;
|
|||
|
end
|
|||
|
else
|
|||
|
Result := Result + LI(0) + '}'#13#10;
|
|||
|
finally
|
|||
|
if jo <> nil then
|
|||
|
jo.Free;
|
|||
|
if jpe <> nil then
|
|||
|
jpe.Free;
|
|||
|
end;
|
|||
|
end;
|
|||
|
|
|||
|
|
|||
|
//<2F><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD>
|
|||
|
function JSON_Fromat_Array(ja: TJSONArray; lv: word = 0): string;
|
|||
|
var
|
|||
|
jae: TJSONArray.TEnumerator;
|
|||
|
jv: TJSONValue;
|
|||
|
js: TJSONString;
|
|||
|
jn: TJSONNumber;
|
|||
|
jb: TJSONBool;
|
|||
|
jo: TJSONObject;
|
|||
|
ja1: TJSONArray;
|
|||
|
S: string;
|
|||
|
begin
|
|||
|
//
|
|||
|
Result := '['#13#10;
|
|||
|
jae := ja.GetEnumerator;
|
|||
|
if jae = nil then
|
|||
|
Exit('');
|
|||
|
|
|||
|
while jae.MoveNext do
|
|||
|
begin
|
|||
|
jv := jae.Current;
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>
|
|||
|
if jv.TryGetValue(jn) then //Number
|
|||
|
Result := Result + LI(lv + 1) + jn.Value + ','#13#10
|
|||
|
else if jv.TryGetValue(jb) then //Boolean
|
|||
|
Result := Result + LI(lv + 1) + jb.Value + ','#13#10
|
|||
|
else if jv.TryGetValue(jo) then //JSONObject
|
|||
|
begin
|
|||
|
Result := Result + LI(lv + 1) + JSON_Format(jo.ToString, lv + 2);
|
|||
|
end
|
|||
|
else if jv.TryGetValue(ja1) then //JSONArray
|
|||
|
Result := Result + LI(lv + 1) + JSON_Fromat_Array(ja1, lv + 1)
|
|||
|
else if jv.TryGetValue(js) then //string
|
|||
|
Result := Result + LI(lv + 1) + '"' + js.Value + '",'#13#10;
|
|||
|
end;
|
|||
|
//ȥ<><C8A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>е<EFBFBD> ,
|
|||
|
S := Result.Substring(Length(Result) - 3, 3);
|
|||
|
if S = ','#13#10 then
|
|||
|
Result := Result.Substring(0, Length(Result) - 3) + #13#10;
|
|||
|
Result := Result + LI(lv) + '],'#13#10;
|
|||
|
|
|||
|
if jae <> nil then
|
|||
|
jae.Free;
|
|||
|
end;
|
|||
|
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>indent
|
|||
|
function LI(lv: Word): string;
|
|||
|
begin
|
|||
|
Result := StringOfChar(' ', lv * Level_indent);
|
|||
|
end;
|
|||
|
|
|||
|
end.
|
|||
|
|