Get JSON return without "result" in Ajax or return without "result" in Delphi

Asked

Viewed 509 times

1

I am generating a JSON in Delphi with mORMot and I need to pick it up on a page to generate some graphics with Google Chart, however the Delphi return me the JSON within a result. I just mount the string into a function with a return of the type RawJSON. He comes this way:

{
  "result":[
    {
      "cols":[
        {"id":"","label":"Mês","pattern":"","type":"string"},
        {"id":"","label":"Quantidade","pattern":"","type":"number"}
      ],
      "rows":[
        {"c":[{"v":"Agosto de 2016","f":null},{"v":191,"f":null}]},
        {"c":[{"v":"Setembro de 2016","f":null},{"v":188,"f":null}]},
        {"c":[{"v":"Outubro de 2016","f":null},{"v":230,"f":null}]},
        {"c":[{"v":"Novembro de 2016","f":null},{"v":243,"f":null}]},
        {"c":[{"v":"Dezembro de 2016","f":null},{"v":145,"f":null}]},
        {"c":[{"v":"Janeiro de 2017","f":null},{"v":245,"f":null}]},
        {"c":[{"v":"Fevereiro de 2017","f":null},{"v":206,"f":null}]},
        {"c":[{"v":"Março de 2017","f":null},{"v":174,"f":null}]},
        {"c":[{"v":"Abril de 2017","f":null},{"v":241,"f":null}]}
      ]
    }
  ]
}

But I need it so that’s what I’m generating:

{
  "cols":[
    {"id":"","label":"Mês","pattern":"","type":"string"},
    {"id":"","label":"Quantidade","pattern":"","type":"number"}
  ],
  "rows":[
    {"c":[{"v":"Agosto de 2016","f":null},{"v":191,"f":null}]},
    {"c":[{"v":"Setembro de 2016","f":null},{"v":188,"f":null}]},
    {"c":[{"v":"Outubro de 2016","f":null},{"v":230,"f":null}]},
    {"c":[{"v":"Novembro de 2016","f":null},{"v":243,"f":null}]},
    {"c":[{"v":"Dezembro de 2016","f":null},{"v":145,"f":null}]},
    {"c":[{"v":"Janeiro de 2017","f":null},{"v":245,"f":null}]},
    {"c":[{"v":"Fevereiro de 2017","f":null},{"v":206,"f":null}]},
    {"c":[{"v":"Março de 2017","f":null},{"v":174,"f":null}]},
    {"c":[{"v":"Abril de 2017","f":null},{"v":241,"f":null}]}
  ]
}

On the page I get the JSON this way:

var jsonData = $.ajax({
      url:      'http://localhost/api/graficos',
      dataType: "json",
      async:    false
    }).responseText;
  • 1

    Console.log(jsonData) I printed what? Ever tried to put jsonData.result? See Fiddle what I did here.

  • It didn’t work, take a look at the Fiddle, i put the code. If you remove "result" from JSON it works.

  • 1

    In Delphi, declare Data.Dbxplatform no uses, in your function where you generate the JSON put the following Getinvocationmetadata() code. Responsecode := 200; Getinvocationmetadata(). Responsecontent := Result.Tostring;

1 answer

0

I was able to solve using the JSON.parse in the result of the request, as it brings a string and the Google Charts awaits an object:

var jsonData = JSON.parse(jsonData);

Then to catch the JSON without the result called it that jsonData.result[0], This shows what’s inside the result.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.