1

I'm using Delphi XE2 with DBXJSON and I'm having trouble handling a JsonArray contained inside a String. The String I got is this:

"[
{DAT_INCL: \"2/3/2012 16:45:9\", NUM_ORDE: 1, NUM_ATND: 734, NUM_ACAO: 2, NUM_RESU: 3},
{DAT_INCL: \"2/3/2012 16:45:10\", NUM_ORDE: 2, NUM_ATND: 734, NUM_ACAO: 4, NUM_RESU: 5},
{DAT_INCL: \"2/3/2012 16:45:10\", NUM_ORDE: 3, NUM_ATND: 734, NUM_ACAO: 8, NUM_RESU: NULL}
]"

Goddamn quotation marks.

No matter how hard I try, I can never get my hands on this array, no ParseJSONValue and no crazy type juggling could get the array out of this quotation-mark prison.

Maybe one of you got the key?

EDIT: This string is already the result of a bit of type conversion. In case anyone is interested in seeing how I extracted this string, maybe to find a better way to finally get the array out (he's really lonely and scared), here's how I did it:

Originally, I had a two-pair TJsonObject named jObject

{
"id": 0,
"data": "[{DAT_INCL: \"08/03/2012 10:07:08\", NUM_ORDE: 1, NUM_ATND: 1, NUM_ACAO: 2, NUM_RESU: 3},
        {DAT_INCL: \"08/03/2012 10:07:09\", NUM_ORDE: 2, NUM_ATND: 1, NUM_ACAO: 4, NUM_RESU: 5},
        {DAT_INCL: \"08/03/2012 10:07:09\", NUM_ORDE: 3, NUM_ATND: 1, NUM_ACAO: 8, NUM_RESU: NULL}]"
}

I proceeded to extract the relevant TJsonPair, named jPair:

jPair := jObject.Get(1);

Which nets me this:

"data": "[{DAT_INCL: \"08/03/2012 10:07:08\", NUM_ORDE: 1, NUM_ATND: 1, NUM_ACAO: 2, NUM_RESU: 3},
        {DAT_INCL: \"08/03/2012 10:07:09\", NUM_ORDE: 2, NUM_ATND: 1, NUM_ACAO: 4, NUM_RESU: 5},
        {DAT_INCL: \"08/03/2012 10:07:09\", NUM_ORDE: 3, NUM_ATND: 1, NUM_ACAO: 8, NUM_RESU: NULL}]"

From that, I extracted the String, named sString

sString:= jPair.JsonValue.ToString;

Which nets me that string on the start of the question. And that is as good as it got, so far.

bpromas
  • 684
  • 1
  • 11
  • 25
  • 1
    why do you need `jPair.JsonValue.ToString`? use `jPair.JsonValue as TJsonArray` to get array – teran Apr 02 '12 at 21:19
  • Your JSON string it seems not valid. – RRUZ Apr 02 '12 at 22:10
  • @RRUZ is right, there is no need to escape doublequotes with backslashes (correct JSON is in my answer here http://stackoverflow.com/a/9616493/1216425 ) – teran Apr 02 '12 at 22:17
  • 1
    possible duplicate of [Delphi: Accessing JSON Objects within a JSON Array](http://stackoverflow.com/questions/9608794/delphi-accessing-json-objects-within-a-json-array) – Wouter van Nifterick Apr 02 '12 at 22:52
  • @Teran, Rruz is right, but not for the reason you state. Quotation marks *do* need to be escaped in JSON because JSON strings are delimited by quotation marks. In this case, we have (faulty) JSON being held *in* a JSON string. – Rob Kennedy Apr 03 '12 at 17:01
  • @RobKennedy, yes, my mistake. so `jPair.JsonValue` is `TJsonString` and we have to use `sString := (jPair.JsonValue as TJsonString).value` to get string without doublequotes, wich we can use to create new `TJsonObject` - `jsa := TJsonObject.ParseJsonValue(sString) as TJsonArray` – teran Apr 03 '12 at 18:23

0 Answers0