#1 2016-11-19 07:55:14

Bibo
Member
Registered: 2016-11-19
Posts: 3

JSON parsing problem

Hi,

I'm doing this:


const
  __MyJSON ='{"waypoints":[{"location":[14.723485,-17.443965],"name":"C46","hint":"m88zgO2Ev4BsywcAtAkAAEQBAACiCAAAXUoAANkSIADlBQAAnangAIPT9f5RnOAAqnT1_gQAAQHOiWch"},' +
            '{"location":[14.759195,-17.448485],"name":"C46","hint":"m88zgO2Ev4BsywcAvgMAAF8CAADuGAAA7D4AANkSIADlBQAAGzXhANvB9f6GOOEAz9r1_gcAAQHOiWch"}],' +
            '"routes":[{"distance":3822.4,"duration":264.6,"legs":[{"distance":3822.4,"duration":264.6,"summary":"","steps":[]}],"geometry":"x_niBytzxAf[e~E"}],"code":"Ok"}';


procedure TForm1.btnParseClick(Sender: TObject);
    var doc : Variant;
    R:Double;
begin
  doc := _JsonFast(__MyJSON);
  R := TDocVariantData(doc).GetValueByPath(['routes','duration']);
end;


Result(R) is 0.
How can I get duration value?
Thanks.

Offline

#2 2016-11-19 09:37:37

jbroussia
Member
From: France
Registered: 2011-04-09
Posts: 74

Re: JSON parsing problem

Routes is an array, which item (position) of the array are you targeting ?

Offline

#3 2016-11-19 12:58:11

Bibo
Member
Registered: 2016-11-19
Posts: 3

Re: JSON parsing problem

Thanks for the answer.

I'm sorry I don't understand the question (I'm playing with JSON and Mormot for the first time). My objective is to get distance and duration values from this Json string I provided. Maybe I'm supposed to answer "First position".

I would like to get distance = 3822.4 and duration = 264.6

I'm trying to use TDocVariantData like in the mORMot documentation because I really like the way it is done (smart and simple without using class or record definition and blazing fast).

From your answer I understand it cannot work without giving the position of the item inside the array. How to do it?

Offline

#4 2016-11-19 20:21:06

jbroussia
Member
From: France
Registered: 2011-04-09
Posts: 74

Re: JSON parsing problem

Arrays are not allowed in paths.
Try this:

R := doc.routes._(0).duration;

Last edited by jbroussia (2016-11-19 20:21:32)

Offline

#5 2016-11-19 22:39:13

Bibo
Member
Registered: 2016-11-19
Posts: 3

Re: JSON parsing problem

This is it!!

Thank you very much.

Offline

#6 2016-12-01 21:00:11

ertank
Member
Registered: 2016-03-16
Posts: 168

Re: JSON parsing problem

Very much like to main question, How can I read first set of "rings" in below Json?

{
"name": "OBJECTID",
"FieldName": "",
"Type": "Polygon",
"fields": [
{
"name": "GID",
"alias": "GID",
"type": "FieldTypeInteger"
}
],
"features": [
{
"attributes": {
"GID": 32289
},
"geometry": {
"rings": [
[
[
47.213439779999987,
41.039492069999994
],
[
47.213007080000011,
41.039838900000007
]
]
] 


}
}
}

What I wrote and didn't work is something like:

procedure TForm1.Button1Click(Sender: TObject);
var
  Json: string;
  Value: Variant;
begin
  Json := Memo1.Text;
  Value := _JsonFast(RawUtf8(Json));
  Label1.Caption := Value.rings._(0);
end;

Error I receive is invalid variant operation.

Thanks.

Offline

#7 2016-12-02 08:12:55

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: JSON parsing problem

Your input is not valid JSON.

Offline

Board footer

Powered by FluxBB