1

I'm trying to parse this json file coming from http://www.weather4u.dk/WordPressium.php:

[{ "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":""} 
]

This code gives me error 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;

the field "description" also needs to be seperated into 2 variables after ||| "description":"Pæn lørdag: Sol til de fleste|||

im totally new to json thanks

3
  • 2
    The JSON data coming from that URL is invalid. It appears the Strings are not being escaped properly. Commented Jun 14, 2014 at 14:15
  • it's not possible to change the code in the php file is there another way to parse the file ? Commented Jun 14, 2014 at 14:30
  • Then I'm afraid you're out of luck without contacting the Vendor of that server and telling them to fix their problem. You shouldn't have to break the rules just because the Vendor breaks the rules. If you really insist on using this data in its current state, no existing JSON library will be able to parse it, and you'd have to write your own parser which follows your Vendor's (non-standard) format. Commented Jun 14, 2014 at 14:40

2 Answers 2

2

That's not JSON and so no amount of JSON parsing will yield any useful information. If you cannot get the supplier to fix their code so that they supply JSON, you'll have to hack away at it.

It seems that the file is created in a line oriented fashion. Probably by some ghastly PHP code that spews out text rather than doing the obvious thing of, well, using a JSON emitter.

So put the content of the file into a string list.

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

Then loop over the text like this:

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;

Now, you'll need to do more work to tidy up all the crufty fake JSON, but this sort of approach should get you going.

Note that I am not advocating this as a nice way to solve the problem. It isn't. It is revolting and makes me feel ill just writing it. But if you simply have to extract content from this data, then you will inevitably have to perform some gnarly ad-hoc parsing.

Sign up to request clarification or add additional context in comments.

Comments

0

The input JSON is invalid (confirmed by the online validator).

  1. Double quote in some values are not escaped with \".
  2. Multi-line string value should be single line string with a soft line break \n. (Thanks Jerry)

Your JSON decoder does nothing wrong, as the critical issue should be fixed in that PHP script. Sure, you can fix the issues using regular expression, before it is sent to your JSON decoder.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.