Delphi如何从HTTP获取解析指定的值

时间:2014-11-21 09:29:01

标签: delphi parsing delphi-xe7

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]);

但我需要自动解析所有值

3 个答案:

答案 0 :(得分:1)

自Delphi 2010以来,您可能会使用Delphi中包含的DBXJSON,另一种选择可能是使用例如Delphi 2010。超对象。
由于您显示的示例是一个没有嵌套的简单JSON字符串,因此只需将TJSONObject作为TJSONArray进行交互即可访问所包含的值。
这个数组包括" Name"和价值 使用您提供的字符串显示的简单示例:

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;

答案 1 :(得分: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;

答案 2 :(得分: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;