【问题标题】:delphi xe6 parse json filedelphi xe6解析json文件
【发布时间】:2014-08-04 22:14:12
【问题描述】:

我正在尝试解析来自 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":""} 
]

这段代码给了我错误invalid class typecast

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æn lørdag: Sol til de fleste|||

我对 json 完全陌生 谢谢

【问题讨论】:

  • 来自该 URL 的 JSON 数据无效。看来字符串没有被正确转义。
  • 无法更改php文件中的代码是否有另一种解析文件的方法?
  • 那么,如果您不联系该服务器的供应商并告诉他们解决问题,恐怕您就不走运了。您不应该仅仅因为供应商违反规则就违反规则。如果您真的坚持使用当前状态下的数据,那么现有的 JSON 库将无法解析它,您必须编写自己的解析器,该解析器遵循您的供应商(非标准)格式。
  • 好的,感谢您的宝贵时间

标签: json delphi parsing


【解决方案1】:

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

  1. 某些值中的双引号不能使用\" 转义。
  2. 多行字符串值应该是带有软换行符\n 的单行字符串。 (谢谢杰瑞)

您的 JSON 解码器没有任何问题,因为关键问题应该在那个 PHP 脚本中得到解决。当然,您可以使用正则表达式修复问题,然后再将其发送到您的 JSON 解码器。

【讨论】:

    【解决方案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,但这种方法应该可以帮助您。

    请注意,我并不是在提倡这是解决问题的好方法。它不是。这是令人反感的,让我觉得只是写它。但是,如果您只需要从这些数据中提取内容,那么您将不可避免地需要执行一些棘手的临时解析。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2015-10-14
      • 2014-07-21
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多