I am trying to parse some JSON that comes back from a REST web service. The return from the get () call is TStringStream. I am using dbxjson to work with data. To simplify the demo here, I created a test project that reproduces the error without calling the web service (instead, it uses a text file to output the web service instead). Here is the code:
var SL : TStringStream; LJsonObj : TJSONObject; begin SL := TStringStream.Create; try SL.LoadFromFile('output.txt'); LJsonObj := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(SL.DataString), 0) as TJSONObject; finally SL.Free; end; end;
Sometimes the phone_numbers array in the JSON data is empty. In a streaming object coming from a web service call, it looks like this:
{ "Contact Information Service": { "response": { "phone_numbers": [ ] } } }
This causes ParseJSONValue to return nil.
However, if I change the empty phone_numbers array to this in my test txt file:
{ "Contact Information Service": { "response": { "phone_numbers": [] } } }
works fine (i.e. returns a TJSONObject). The difference is a space in an empty array. For some reason, the first JSON response with a space in an empty array causes ParseJSONValue to return zero. It works fine without spaces between square brackets.
What am I doing wrong with my JSON analysis? Is there some kind of preliminary parsing I need to do before calling ParseJSONValue?
Sam m
source share