Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // JSON Parser - one level parse Array or Object to WStrList
- type
- TJsPair = record
- Name:string;
- Value:string;
- end;
- function IsJsonArray(const S: String): Boolean;
- var
- Len: Integer;
- begin
- Len := Length(S);
- Result := (Len >= 2) and (S[1] = '[') and (S[Len] = ']');
- end;
- function IsJsonObject(const S: String): Boolean;
- var
- Len: Integer;
- begin
- Len := Length(S);
- Result := (Len >= 2) and (S[1] = '{') and (S[Len] = '}');
- end;
- function IsJsonString(const S: String): Boolean;
- var
- Len: Integer;
- begin
- Len := Length(S);
- Result := (Len >= 2) and (S[1] = '"') and (S[Len] = '"');
- end;
- function JsonParse(JsonString:string; OutList:PWStrList):Boolean;
- procedure Split(const S: String; const Delimiter: Char;
- Strings: PWStrList);
- function IsPairBegin(C: Char): Boolean;
- begin
- Result := (C = '{') or (C = '[') or (C = '"');
- end;
- function GetPairEnd(C: Char): Char;
- begin
- case C of
- '{': Result := '}';
- '[': Result := ']';
- '"': Result := '"';
- else Result := #0;
- end;
- end;
- function MoveToPair(P: PChar): PChar;
- var
- PairBegin, PairEnd: Char;
- C: Char;
- begin
- PairBegin := P^;
- PairEnd := GetPairEnd(PairBegin);
- Result := P;
- while Result^ <> #0 do
- begin
- Inc(Result);
- C := Result^;
- if C = PairEnd then Break
- else if (PairBegin = '"') and (C = '\') then Inc(Result)
- else if (PairBegin <> '"') and IsPairBegin(C) then Result := MoveToPair(Result);
- end;
- end;
- var
- PtrBegin, PtrEnd: PChar;
- C: Char;
- StrItem: String;
- begin
- PtrBegin := PChar(S);
- PtrEnd := PtrBegin;
- while PtrEnd^ <> #0 do
- begin
- C := PtrEnd^;
- if C = Delimiter then
- begin
- StrItem := Trim(Copy(PtrBegin, 1, PtrEnd - PtrBegin));
- Strings.Add(StrItem);
- PtrBegin := PtrEnd + 1;
- PtrEnd := PtrBegin;
- Continue;
- end
- else if IsPairBegin(C) then PtrEnd := MoveToPair(PtrEnd);
- Inc(PtrEnd);
- end;
- StrItem := Trim(Copy(PtrBegin, 1, PtrEnd - PtrBegin));
- if StrItem <> '' then Strings.Add(StrItem);
- end;
- function JsPairParse(JsonString: String):TJsPair;
- var
- List: PWStrList;
- StrName, StrVal: String;
- begin
- Result.Name := '';
- Result.Value := '';
- List := NewWStrList;
- try
- Split(JsonString, ':', List);
- if List.Count >= 2 then
- begin
- StrName := List.Items[0];
- StrVal := List.Items[1];
- Result.Name := Copy(StrName, 2, Length(StrName) - 2);
- if IsJsonString(StrVal) then // Remove " "
- Result.Value := Copy(StrVal, 2, Length(StrVal) - 2)
- else
- Result.Value := StrVal;
- end;
- finally
- List.Free;
- end;
- end;
- var
- I: Integer;
- S: String;
- List: PWStrList;
- Item: TJsPair;
- isJO, isJA: Boolean;
- begin
- OutList.Clear;
- JsonString := Trim(JsonString);
- isJO := IsJsonObject(JsonString);
- isJA := IsJsonArray(JsonString);
- if isJO or isJA then
- begin
- S := Trim(Copy(JsonString, 2, Length(JsonString) - 2));
- List := NewWStrList;
- List.Clear;
- try
- Split(S, ',', List);
- for I := 0 to List.Count - 1 do
- begin
- if isJO then
- begin
- Item := JsPairParse(List.Items[I]);
- OutList.Values[Item.Name] := Item.Value;
- end else
- if isJA then
- begin
- S := List.Items[I];
- if IsJsonString(S) then // Remove " "
- OutList.Add(Copy(S, 2, Length(S) - 2))
- else
- OutList.Add(S);
- end;
- end;
- Result:=True;
- finally
- List.Free;
- end;
- end else begin
- Result:=False;
- exit;
- end;
- end;
- // JSON Parser end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement