【发布时间】: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 库将无法解析它,您必须编写自己的解析器,该解析器遵循您的供应商(非标准)格式。
-
好的,感谢您的宝贵时间