delphi xe6解析json文件

时间:2014-06-14 14:13:36

标签: json delphi parsing

我正在尝试解析来自http://www.weather4u.dk/WordPressium.php的这个json文件:

[{ "id":33, 
   "title":"Indland", 
   "longitude":"", 
   "description":"Pæn lørdag: Sol til de fleste|||<a href="http://www.weather4u.dk/wp-content/uploads/2014/06/41134779-894e6eebfef3609ee67486398c9638671.jpeg"><img class="alignnone size-full wp-image-34" src="http://www.weather4u.dk/wp-content/uploads/2014/06/41134779-894e6eebfef3609ee67486398c9638671.jpeg" alt="41134779-894e6eebfef3609ee67486398c9 ..|||2014-06-14 13:11:35|||http://www.weather4u.dk/wp-content/uploads/2014/06/41134779-894e6eebfef3609ee67486398c9638671.jpeg|||http://www.weather4u.dk/?p=33|||", 
   "upload_date":"2014-06-14 13:11:35", 
   "thumbnail_medium":"http://www.weather4u.dk/wp-content/uploads/2014/06/41134779-894e6eebfef3609ee67486398c9638671.jpeg", 
   "latitude":""
 },
 { "id":12, 
   "title":"Indland", 
   "longitude":"", 
   "description":"Nu kommer det tørre og lune sommervejr tilbage|||Et voldsomt uvejr drog hen over området torsdag aften akkurat som mange af indbyggerne i den lille landsby Berner Emmental havde sat sig til rette foran fjernsynet for at se åbningskampen ved VM i fodbold. ..|||2014-06-12 14:37:41|||http://www.weather4u.dk/wp-content/uploads/2014/06/41129590-1082663f300d83812527d384fc46a7481.jpeg|||http://www.weather4u.dk/?p=12|||", "upload_date":"2014-06-12 14:37:41", "thumbnail_medium":"http://www.weather4u.dk/wp-content/uploads/2014/06/41129590-1082663f300d83812527d384fc46a7481.jpeg", 
   "latitude":""
 }, 
 { "id":5, 
   "title":"Indland", 
   "longitude":"", 
   "description":"Se billederne: Dramatisk 'bølgehimmel' før tordenbyger|||Ud over kraftig regn, så faldt der hagl på størrelse med valnødder og i så enorme mængder, at landsbyens gader nærmest så ud som om, at byen var blevet ramt af en pludselig snestorm. ..|||2014-06-12 11:34:46|||http://www.weather4u.dk/wp-content/uploads/2014/06/41130401-e98757232f1427b047a5bb0b4c8f3cd21.jpeg|||http://www.weather4u.dk/?p=5|||", "upload_date":"2014-06-12 11:34:46", "thumbnail_medium":"http://www.weather4u.dk/wp-content/uploads/2014/06/41130401-e98757232f1427b047a5bb0b4c8f3cd21.jpeg", 
   "latitude":""} 
]

此代码错误无效类别类型

var
  LJsonObj   : TJSONObject;
  LJsonValue : TJSONValue;
  LJsonArray : TJsonArray;
  LJPair    : TJSONPair;
  Ltitle    : TJSONString;
  Ldescription  : TJSONValue;
  Lupload_date     : TJSONValue;
  Lid       : Integer;
  LIndex    : Integer;
  LSize     : Integer;
mydata : string;

begin
    mydata := GetURLAsString('http://www.weather4u.dk/WordPressium_Category.php');
     LJsonObj    := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(mydata),0) as TJSONObject;
  try
     Ltitle:=LJsonObj.Get('title').JsonString;
     Label1.Text:= LJsonValue.Value;
  finally
     LJsonObj.Free;
  end;

字段“description”也需要在|||之后分成2个变量 “description”:“Pænlørdag:Sol til de fleste |||

我对json来说是全新的 感谢

2 个答案:

答案 0 :(得分:2)

那不是JSON,因此没有任何数量的JSON解析会产生任何有用的信息。如果您无法让供应商修改他们的代码以便他们提供JSON,那么您将不得不抨击它。

似乎该文件是以面向行的方式创建的。可能是一些令人毛骨悚然的PHP代码,它们使用JSON发射器来喷出文本而不是做明显的事情。

所以把文件的内容放到字符串列表中。

Lines := TStringList.Create;
Lines.Text := GetURLAsString(...);

然后循环遍历文本:

for i := 1 to Lines.Count-1 do // ignore the first line
begin
  Line := Trim(Lines[i]);
  P := Pos(':', Line);
  Key := Copy(Line, 1, P-1);
  Value := Copy(Line, P+1, MaxInt);
  .... use Key and Value
end;

现在,你需要做更多的工作来整理所有狡猾的假JSON,但这种方法应该让你前进。

请注意,我并不是主张将此作为解决问题的好方法。它不是。它令人反感,让我感到生病只是写它。但是如果你只需要从这些数据中提取内容,那么你将不可避免地要进行一些粗略的特殊解析。

答案 1 :(得分:0)

输入JSON无效(由the online validator确认)。

  1. 某些值中的双引号不会使用\"进行转义。
  2. 多行字符串值应为带有换行符\n的单行字符串。 (谢谢Jerry)
  3. 您的JSON解码器没有任何错误,因为应该在该PHP脚本中修复关键问题。当然,您可以在将问题发送到JSON解码器之前使用正则表达式解决问题。