• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to parse a JSON file and extract all the polylines in a list?

 
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I am trying to parse a JSON file that contains polylines and points. The thing is it returns only the first value of the first object and stops. Am I missing something in the loop? I need all the polylines so I can display them on the map. Cheers!





 
Saloon Keeper
Posts: 15731
368
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post example input, expected output, and actual output.

Like I said in your other topic, it's easier to process JSON using a binding API like Jackson or JSON-B. Parsing it by hand is like writing your application in assembly code.
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Example input:

These are polylines from the JSON files. Like: "points" : "{kd_IljoJ" , "points" : "{kd_IljoJd@L|Ab@pA\\r@P", "points" : "qcd_InmoJBSNoAd@}DVsBJk@He@@ERiAPeA@MFi@BMFa@Lu@Hk@",  "points" : "mgc_IltnJLe@DI|B}ELU" ... These are polylines in a order and expected output.

And now that's the actual output: "points": "{kd_IljoJ"

As you can see only the first value and stops there.

I played around with the JSON and noticed that when I change the int value of the getJSONObject(0) ti (1) it gives consequent output. That means that I have to somehow loop through all of the getJSONObject(i), but I can't make it work.

That's the JSON itself for you to have a look at ->
https://gist.github.com/Biseamon/dad9e9c5b91b68d4caebcfa8af7a7eb8
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding the Jackson, I will have a look right now? but if you got any helpful docs I would highly appreciate that.
 
Stephan van Hulst
Saloon Keeper
Posts: 15731
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that you're only getting the steps of the first leg, and the first leg only has one step.

You probably want to get the steps of all legs of all routes.

Here is the JSON-B user guide. It's not very difficult to use: http://json-b.net/docs/user-guide.html
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So far I have done some changes and it seems to work, however it shows the polylines as straight lines which is strange. Aslo, I don't know how to test the code to see if the loop doesn't return duplicate values. Is there a way to check that?

Thank you for your recommendations by the way. I might look into JSON-B eventually if I won't make it with this one current.

 
Stephan van Hulst
Saloon Keeper
Posts: 15731
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marin Capranov wrote:it shows the polylines as straight lines which is strange.


I can't comment to this because I don't know the PolyUtil.decode implementation, nor do I know what you do with the movements list afterwards.

Aslo, I don't know how to test the code to see if the loop doesn't return duplicate values. Is there a way to check that?


Duplicate values for what? Points on the map? Why wouldn't you allow duplicates? Is it not possible to arrive at the same point on the map twice?

Note that flattening the routes and legs to a single list of points will make it look like the ending point of one leg is also the starting point of the next leg. I'm not sure this is what you want.
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your hints. They helped me to find solutions and now I can move to the next phase, which is the genetic algorithm =)
 
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic