0
procedure TMainForm.btn1Click(Sender: TObject);
var
  lHTTP: TIdHTTP;
begin
  lHTTP := TIdHTTP.Create(nil);
  try
    mmo1.Text := lHTTP.Get('http://guessit.io/guess?filename=House.of.Cards.2013.S02E03.1080p.NF.WEBRip.DD5.1.x264-NTb.mkv');
  finally
    lHTTP.Free;
  end;
end;

结果将是这样的:

{
   "series":"House of Cards",
   "episodeNumber":3,
   "releaseGroup":"NTb",
   "format":"WEBRip",
   "season":2,
   "audioCodec":"DolbyDigital",
   "year":2013,
   "mimetype":"video/x-matroska",
   "container":"mkv",
   "videoCodec":"h264",
   "other":[
      "Netflix"
   ],
   "audioChannels":"5.1",
   "screenSize":"1080p",
   "type":"episode"
}

例如,我如何阅读系列(“系列”:“纸牌屋”)值。

我尝试使用

Function ExtractBetweenTags(Const Line, TagI, TagF: string): string;
var
  i, f : integer;
begin
  i := Pos(TagI, Line);
  f := Pos(TagF, Copy(Line, i+length(TagI), MAXINT));
  if (i > 0) and (f > 0) then
    Result:= Copy(Line, i+length(TagI), f-1);
end;

mmo1.Text := StringReplace(ExtractBetweenTags(mmo1.Text,'"series": "','"'), ' ', '-',[rfReplaceAll, rfIgnoreCase]);

但我需要自动解析所有值

4

3 回答 3

1

自 Delphi 2010 起,您可能会使用 Delphi 中包含的 DBXJSON,另一种选择可能是使用例如 SuperObject。
由于您显示的示例是一个没有嵌套的简单 JSON 字符串,因此您可以通过访问作为 TJSONArray 访问的 TJSONObject 来访问共同包含的值。
该数组由成对的“名称”和值组成。
使用您提供的字符串显示的一个简单示例:

uses
DBXJSON;

procedure TForm7.Button1Click(Sender: TObject);
var
  S:String;
  I:Integer;
  JObj:TJSONObject;
  Pair : TJSONPair;
begin
  s := '{"series": "House of Cards", "episodeNumber": 3, "releaseGroup": "NTb", "format": "WEBRip", "season": 2'
        +', "audioCodec": "DolbyDigital", "year": 2013, "mimetype": "video/x-matroska", "container": "mkv", "videoCodec": "h264", "other": ["Netflix"], "audioChannels": 

"5.1", "screenSize": "1080p", "type": "episode"}';
  JObj := TJSONObject.ParseJSONValue(s) as TJSONObject;
  for I := 0 to TJSONArray(JObj).Size - 1 do
    begin
       Pair := TJSONPair(TJSONArray(JObj).Get(i));
       Memo1.Lines.Add( Pair.JsonString.Value + '=' + Pair.JsonValue.ToString)
    end;
end;
于 2014-11-21T11:03:57.887 回答
1

您可以使用如下类解析对象:

uses System.JSON, Rest.JSON;

type
  TGuess = class(TObject)
  public
    fseries: string;
    fepisodeNumber: integer;
    freleaseGroup: string;
    fformat: string;
    fseason: integer;
    faudioCodec: string;
    fyear: integer;
    fmimetype: string;
    fcontainer: string;
    fvideoCodec: string;
    fother: TArray<string>;
    faudioChannels: string;
    fscreenSize: string;
    ftype: string;
  end;

var
  g: TGuess;
  s: string; // your input string

  // g object will be created by following call and will contain parsed values
  g := TJson.JsonToObject<TGuess>(s); 

  // do something with g

  // release g after it is no longer needed
  g.Free;
于 2014-11-21T14:20:21.930 回答
0

感谢所有的答案,但在这里我终于弄清楚了

procedure TMainForm.btn1Click(Sender: TObject);
var
  S : String;
  NS , S1,S2,series,season,episodeNumber : String;
  L , I , LN : integer;
  MM :TStringList;
begin
  try
    MM := TStringList.Create;
    NS := '';
    S := '{"series": "House of Cards", "episodeNumber": 3, "releaseGroup": "NTb", "format":'+
    '"WEBRip", "season": 2, "audioCodec": "DolbyDigital", "year": 2013, "mimetype": "video/x-matroska", "container": "mkv",'+
    '"videoCodec": "h264", "other": ["Netflix"], "audioChannels": "5.1", "screenSize": "1080p", "type": "episode"}';
     {if AnsiPos('title',s) <> 0 then
     IsMovie := True
     else
     IsMovie := False;}

    L := Length(S);

    for I := 1 to L do
        Begin
            If (Ord(S[I]) <> 34)
            And (Ord(S[I]) <> 123)
            And (Ord(S[I]) <> 125)
            Then NS := NS + Copy(S,I,1);
        End;
        S := NS;
        NS := '';
        L := Length(S);

    for I := 1 to L do
        Begin
            If (Ord(S[I]) = 44)
            Or (Ord(S[I]) = 58)
            Then Begin
              NS := NS + #13#10;
              MM.Add(Trim(NS));
              NS := '';
            End
            Else NS := NS + Copy(S,I,1);
        End;
        MM.Add(Trim(NS));
//      --------------------------------------------------------------

        LN := MM.Count-1;
        for I := 0 to LN Do
        Begin
            If MM[I] = Trim('series')
            Then series := Trim(MM[I + 1]);
            If MM[I] = Trim('season')
            Then season := Trim(MM[I + 1]);
            If MM[I] = Trim('episodeNumber')
            Then episodeNumber := Trim(MM[I + 1]);
        End;

        ShowMessage(series+'.S'+season+'.E'+episodeNumber);

  finally
    MM.Free
  end;
end;
于 2014-11-21T16:44:02.960 回答