1. Ong Chin Keat
  2. PowerBuilder
  3. Tuesday, 30 January 2024 06:20 AM UTC

hi guru,

PowerBuilder Runtime version : PB2022 R2 build 2828

I'm trying to use jsonpackage to extract value for "distance" and "duration"...but seem the result was not correct.  Please help to advise what wrong with the parser...many thanks.

 The code as below : 

String ls_Body , ls_URL, ls_start, ls_end, ls_value
INTEGER li_return
HttpClient lhc_Client
jsonpackage lnv_pack1,lnv_pack2,lnv_pack3,lnv_pack4,lnv_pack5

lnv_pack1 = create jsonpackage
lnv_pack2 = create jsonpackage
lnv_pack3 = create jsonpackage

ls_url = "http://api.openrouteservice.org/v2/directions/driving-hgv?api_key=5b3ce3597851110001cf6248a70942dce6df4c7dad59dc4c41dee25d&start=100.39488627777779,5.371609022222223&end=100.48055997985934,5.339213073913044";

li_Return = lhc_Client.SendRequest("GET", ls_URL)
IF li_Return = 1 THEN
lhc_Client.GetResponseBody(ls_Body)

lnv_pack1.loadstring(ls_Body)
ls_value = lnv_pack1.getvalue("features")
IF LEFT(ls_value,1) = '[' THEN
ls_value = Mid(ls_value,2,len(ls_value) -2)
END IF
lnv_pack2.loadstring( ls_value)
ls_value = lnv_pack2.getvalue("properties")
lnv_pack3.loadstring( ls_value)
ls_value = lnv_pack3.getvalue("summary") //get "{"distance":17292.7,"duration":1212.1}"
ls_value = lnv_pack3.getvalue("distance") //get NULL
ls_value = lnv_pack3.getvalue("duration") //get NULL

ls_value = STRING(lnv_pack3.getvalue("distance")) //get NULL
ls_value = STRING(lnv_pack3.getvalue("duration")) //get NULL

END IF

The Json script generated at Body as below : 

{
"type": "FeatureCollection",
"metadata": {
"attribution": "openrouteservice.org | OpenStreetMap contributors",
"service": "routing",
"timestamp": 1706594016867,
"query": {
"coordinates": [
[
100.39488627777779,
5.371609022222223
],
[
100.48055997985934,
5.339213073913044
]
],
"profile": "driving-hgv",
"format": "json"
},
"engine": {
"version": "7.1.0",
"build_date": "2023-12-10T05:30:50Z",
"graph_date": "2024-01-28T13:11:26Z"
}
},
"bbox": [
100.38867,
5.328206,
100.481411,
5.378329
],
"features": [
{
"bbox": [
100.38867,
5.328206,
100.481411,
5.378329
],
"type": "Feature",
"properties": {
"transfers": 0,
"fare": 0,
"segments": [
{
"distance": 17292.7,
"duration": 1212.1,
"steps": [
{
"distance": 484.8,
"duration": 34.9,
"type": 11,
"instruction": "Head northwest on Lorong Perusahaan 1",
"name": "Lorong Perusahaan 1",
"way_points": [
0,
5
]
},
{
"distance": 580.0,
"duration": 74.1,
"type": 0,
"instruction": "Turn left onto Lorong Perusahaan 3",
"name": "Lorong Perusahaan 3",
"way_points": [
5,
11
]
},
{
"distance": 706.5,
"duration": 67.2,
"type": 0,
"instruction": "Turn left onto Jalan Perusahaan, 3112",
"name": "Jalan Perusahaan, 3112",
"way_points": [
11,
23
]
},
{
"distance": 106.4,
"duration": 8.5,
"type": 12,
"instruction": "Keep left",
"name": "-",
"way_points": [
23,
27
]
},
{
"distance": 1550.9,
"duration": 143.5,
"type": 13,
"instruction": "Keep right",
"name": "-",
"way_points": [
27,
48
]
},
{
"distance": 8286.0,
"duration": 393.9,
"type": 12,
"instruction": "Keep left",
"name": "-",
"way_points": [
48,
180
]
},
{
"distance": 3410.0,
"duration": 292.5,
"type": 2,
"instruction": "Turn sharp left onto Jalan Bukit Minyak, P178",
"name": "Jalan Bukit Minyak, P178",
"way_points": [
180,
243
]
},
{
"distance": 36.6,
"duration": 8.8,
"type": 12,
"instruction": "Keep left",
"name": "-",
"way_points": [
243,
247
]
},
{
"distance": 1088.0,
"duration": 92.7,
"type": 4,
"instruction": "Turn slight left onto Jalan Desa Palma 1",
"name": "Jalan Desa Palma 1",
"way_points": [
247,
271
]
},
{
"distance": 731.4,
"duration": 47.9,
"type": 1,
"instruction": "Turn right onto Jalan Damai",
"name": "Jalan Damai",
"way_points": [
271,
277
]
},
{
"distance": 176.0,
"duration": 18.2,
"type": 0,
"instruction": "Turn left onto Jalan Kilang Ubi",
"name": "Jalan Kilang Ubi",
"way_points": [
277,
281
]
},
{
"distance": 89.8,
"duration": 21.6,
"type": 0,
"instruction": "Turn left",
"name": "-",
"way_points": [
281,
285
]
},
{
"distance": 46.3,
"duration": 8.3,
"type": 0,
"instruction": "Turn left",
"name": "-",
"way_points": [
285,
286
]
},
{
"distance": 0.0,
"duration": 0.0,
"type": 10,
"instruction": "Arrive at your destination, on the left",
"name": "-",
"way_points": [
286,
286
]
}
]
}
],
"way_points": [
0,
286
],
"summary": {
"distance": 17292.7,
"duration": 1212.1
}
},
"geometry": {
"coordinates": [
[
100.394862,
5.371562
],
[
100.394755,
5.371616
],
[
100.393959,
5.37205
],
[
100.393143,
5.372494
],
[
100.391641,
5.373301
],
[
100.391024,
5.373659
],
[
100.389954,
5.371694
],
[
100.389862,
5.371526
],
[
100.38929,
5.370475
],
[
100.388687,
5.369368
],
[
100.38867,
5.369191
],
[
100.388674,
5.369034
],
[
100.388995,
5.368861
],
[
100.389388,
5.368653
],
[
100.389764,
5.368447
],
[
100.390901,
5.367845
],
[
100.391004,
5.367783
],
[
100.392199,
5.367142
],
[
100.39263,
5.366919
],
[
100.392913,
5.366771
],
[
100.393278,
5.366577
],
[
100.393487,
5.366467
],
[
100.39376,
5.366336
],
[
100.394323,
5.366083
],
[
100.394554,
5.366046
],
[
100.394761,
5.366039
],
[
100.394953,
5.366055
],
[
100.395274,
5.366121
],
[
100.396272,
5.366326
],
[
100.396414,
5.366322
],
[
100.396643,
5.36629
],
[
100.396862,
5.366303
],
[
100.396999,
5.366344
],
[
100.397175,
5.366442
],
[
100.397286,
5.366539
],
[
100.397332,
5.366598
],
[
100.397927,
5.367497
],
[
100.398086,
5.367709
],
[
100.39827,
5.367906
],
[
100.398794,
5.368672
],
[
100.399362,
5.369579
],
[
100.399517,
5.369807
],
[
100.399651,
5.369966
],
[
100.400159,
5.37091
],
[
100.400906,
5.37233
],
[
100.401374,
5.37322
],
[
100.402268,
5.375247
],
[
100.402872,
5.376637
],
[
100.40297,
5.376901
],
[
100.403063,
5.377387
],
[
100.403074,
5.377604
],
[
100.403058,
5.377758
],
[
100.403015,
5.377924
],
[
100.402932,
5.378058
],
[
100.402765,
5.378224
],
[
100.402633,
5.378291
],
[
100.402433,
5.378329
],
[
100.402237,
5.378299
],
[
100.402088,
5.378234
],
[
100.401947,
5.378119
],
[
100.401827,
5.377932
],
[
100.401781,
5.377764
],
[
100.401778,
5.377564
],
[
100.401856,
5.377353
],
[
100.402016,
5.377136
],
[
100.40235,
5.37683
],
[
100.403308,
5.376289
],
[
100.404106,
5.375872
],
[
100.406115,
5.374842
],
[
100.406522,
5.374627
],
[
100.407545,
5.374042
],
[
100.407841,
5.373878
],
[
100.408175,
5.373742
],
[
100.40843,
5.373644
],
[
100.408578,
5.373555
],
[
100.409692,
5.372867
],
[
100.41068,
5.372343
],
[
100.411663,
5.371812
],
[
100.412753,
5.371231
],
[
100.41315,
5.371019
],
[
100.413657,
5.37075
],
[
100.414333,
5.370377
],
[
100.415564,
5.369716
],
[
100.416403,
5.369288
],
[
100.416956,
5.368994
],
[
100.417545,
5.368654
],
[
100.417724,
5.368544
],
[
100.418496,
5.368133
],
[
100.418778,
5.367983
],
[
100.41993,
5.36737
],
[
100.42248,
5.366037
],
[
100.423053,
5.365737
],
[
100.424723,
5.364899
],
[
100.424959,
5.36473
],
[
100.425169,
5.364479
],
[
100.425282,
5.364265
],
[
100.425418,
5.363831
],
[
100.426201,
5.360862
],
[
100.426424,
5.360039
],
[
100.426533,
5.359679
],
[
100.426818,
5.358762
],
[
100.426835,
5.358704
],
[
100.427112,
5.357764
],
[
100.427698,
5.355825
],
[
100.427712,
5.355778
],
[
100.427761,
5.355622
],
[
100.427779,
5.355562
],
[
100.427953,
5.355037
],
[
100.428048,
5.354863
],
[
100.42823,
5.354598
],
[
100.428467,
5.3543519999999999
],
[
100.428685,
5.354183
],
[
100.428846,
5.354092
],
[
100.42905,
5.353999
],
[
100.430313,
5.353537
],
[
100.430708,
5.353345
],
[
100.430922,
5.353204
],
[
100.431006,
5.353149
],
[
100.431308,
5.352955
],
[
100.431801,
5.352639
],
[
100.432198,
5.352391
],
[
100.43263,
5.352085
],
[
100.432905,
5.351745
],
[
100.433024,
5.351538
],
[
100.433153,
5.351257
],
[
100.433273,
5.35102
],
[
100.433488,
5.350543
],
[
100.433652,
5.350215
],
[
100.433721,
5.350091
],
[
100.433855,
5.349849
],
[
100.433931,
5.349747
],
[
100.434245,
5.349327
],
[
100.434391,
5.349141
],
[
100.434466,
5.349009
],
[
100.434579,
5.348778
],
[
100.434697,
5.3485
],
[
100.434771,
5.348191
],
[
100.434806,
5.347756
],
[
100.434818,
5.347282
],
[
100.434821,
5.347175
],
[
100.43483,
5.346826
],
[
100.43484,
5.346449
],
[
100.434851,
5.346251
],
[
100.434878,
5.346078
],
[
100.434932,
5.345861
],
[
100.43496,
5.345707
],
[
100.435108,
5.345336
],
[
100.435425,
5.344762
],
[
100.436013,
5.34373
],
[
100.436253,
5.343339
],
[
100.436598,
5.342843
],
[
100.436923,
5.342395
],
[
100.437483,
5.341685
],
[
100.4379,
5.34119
],
[
100.438159,
5.340917
],
[
100.439076,
5.340004
],
[
100.439719,
5.33939
],
[
100.439889,
5.339239
],
[
100.440795,
5.338429
],
[
100.441349,
5.337956
],
[
100.441672,
5.337655
],
[
100.441918,
5.337432
],
[
100.442142,
5.337239
],
[
100.443028,
5.336483
],
[
100.443711,
5.335902
],
[
100.44422,
5.335455
],
[
100.445917,
5.333902
],
[
100.446085,
5.333725
],
[
100.446217,
5.333549
],
[
100.446427,
5.333171
],
[
100.446488,
5.333056
],
[
100.44673,
5.332505
],
[
100.447145,
5.331612
],
[
100.447343,
5.331151
],
[
100.447445,
5.330897
],
[
100.447583,
5.330501
],
[
100.447668,
5.330133
],
[
100.447715,
5.329829
],
[
100.447738,
5.329388
],
[
100.447753,
5.328884
],
[
100.447799,
5.328206
],
[
100.448038,
5.328371
],
[
100.448188,
5.328508
],
[
100.448932,
5.329354
],
[
100.449218,
5.329761
],
[
100.449385,
5.329971
],
[
100.449659,
5.330314
],
[
100.450073,
5.33084
],
[
100.450162,
5.330954
],
[
100.450181,
5.33098
],
[
100.450371,
5.331224
],
[
100.450945,
5.331949
],
[
100.451671,
5.332902
],
[
100.452037,
5.333363
],
[
100.452479,
5.333937
],
[
100.452519,
5.333989
],
[
100.453242,
5.334912
],
[
100.453338,
5.335038
],
[
100.453939,
5.335825
],
[
100.454494,
5.33657
],
[
100.454563,
5.336644
],
[
100.454737,
5.336763
],
[
100.454837,
5.336786
],
[
100.455047,
5.336877
],
[
100.455237,
5.336951
],
[
100.455469,
5.336994
],
[
100.456001,
5.336923
],
[
100.456117,
5.336907
],
[
100.456308,
5.336875
],
[
100.456754,
5.336792
],
[
100.457014,
5.336748
],
[
100.457511,
5.336659
],
[
100.459249,
5.336353
],
[
100.460154,
5.336206
],
[
100.460444,
5.336145
],
[
100.461453,
5.335952
],
[
100.461569,
5.335926
],
[
100.46177,
5.335882
],
[
100.462307,
5.335777
],
[
100.463189,
5.335617
],
[
100.463307,
5.335595
],
[
100.464032,
5.335455
],
[
100.464082,
5.335446
],
[
100.464167,
5.33543
],
[
100.464333,
5.335442
],
[
100.464623,
5.335416
],
[
100.464821,
5.335379
],
[
100.464988,
5.335319
],
[
100.465576,
5.335216
],
[
100.466953,
5.334956
],
[
100.468567,
5.334667
],
[
100.468929,
5.33459
],
[
100.469191,
5.334511
],
[
100.469442,
5.3344
],
[
100.469701,
5.334253
],
[
100.469984,
5.334052
],
[
100.471359,
5.33298
],
[
100.47154,
5.332865
],
[
100.471727,
5.332774
],
[
100.47201,
5.332671
],
[
100.472171,
5.332628
],
[
100.472575,
5.332552
],
[
100.473068,
5.332461
],
[
100.473586,
5.332367
],
[
100.473708,
5.332397
],
[
100.473758,
5.332426
],
[
100.473805,
5.332463
],
[
100.473853,
5.332535
],
[
100.473892,
5.332765
],
[
100.473932,
5.333006
],
[
100.473979,
5.333281
],
[
100.474021,
5.333531
],
[
100.474065,
5.333789
],
[
100.474108,
5.334044
],
[
100.47434,
5.335413
],
[
100.474483,
5.335804
],
[
100.47449,
5.335901
],
[
100.474453,
5.335947
],
[
100.473738,
5.336208
],
[
100.473591,
5.33629
],
[
100.473405,
5.336477
],
[
100.473254,
5.336686
],
[
100.47316,
5.336892
],
[
100.473112,
5.337117
],
[
100.4731,
5.337389
],
[
100.473126,
5.337594
],
[
100.47323,
5.337832
],
[
100.473824,
5.338739
],
[
100.474019,
5.339036
],
[
100.474929,
5.340423
],
[
100.475077,
5.340648
],
[
100.475235,
5.34089
],
[
100.475808,
5.340522
],
[
100.476485,
5.340096
],
[
100.478157,
5.339034
],
[
100.479608,
5.338113
],
[
100.479978,
5.337876
],
[
100.480805,
5.337352
],
[
100.480959,
5.337748
],
[
100.481059,
5.337997
],
[
100.481359,
5.33872
],
[
100.481411,
5.338815
],
[
100.481161,
5.338993
],
[
100.481142,
5.339065
],
[
100.481083,
5.339319
],
[
100.48095000000001,
5.33942
],
[
100.480537,
5.339351
]
],
"type": "LineString"
}
}
]
}

best regards,

Dev Ong

 

 

Accepted Answer
Andreas Mykonios Accepted Answer Pending Moderation
  1. Tuesday, 30 January 2024 07:47 AM UTC
  2. PowerBuilder
  3. # Permalink

Hi.

First of all, when you have such long strings (like your json) it is better to put it as an attachment. Just to help other people who want to see your question and try to help you. You can also read the Advice at https://community.appeon.com/index.php/qna/q-a/q-a-forum-tip-including-source-code-in-your-question to see a better way to place your code in your question.

The main problem is you try to read a numeric value using getvalue. You should use getvaluenumber.

I use the following json as example:

{
	"distance": 17292.7,
	"duration": 1212.1
}

I run the following code and it works fine:

double ld_res
string ls_json_string, ls_res
JSONPackage lnv_jsonpackage

ls_json_string = "{ " + &
"~"distance~": 17292.7, " + &
"~"duration~": 1212.1 " + &
"}"
lnv_jsonpackage = create JSONPackage
lnv_jsonpackage.loadstring(ls_json_string)

try
	ld_res = lnv_jsonpackage.getvaluenumber("distance")
	messagebox("distance: ", ld_res)
	ld_res = lnv_jsonpackage.getvaluenumber("duration")
	messagebox("duration: ", ld_res)
catch (throwable exc1)
	messagebox("", exc1.text)
finally
	if isvalid(lnv_jsonpackage) then destroy lnv_jsonpackage
end try

Andreas.

Comment
There are no comments made yet.
Ong Chin Keat Accepted Answer Pending Moderation
  1. Tuesday, 30 January 2024 11:23 AM UTC
  2. PowerBuilder
  3. # 1

Thanks Mr Andreas Mykonios. I solved my problem. Noted on your advise, will do attachment next time. Thanks.

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.