Jump to content

JSON polyline points parser

Terar

 I want to parse the entire JSON file that contains many JSONObjects "steps". I need to extract all polylines and their points. However, it returns only the very first value and nothing more. Is anything wrong with my loop or I just parse the JSON file wrongly? Thx

 

code snippet

Link to comment
Share on other sites

Link to post
Share on other sites

could u maybe post an example json file?

i didnt parse json "by hand" in a long time but my guess would be on line 16

https://gist.github.com/Biseamon/64515eefec707a9c3cb4fcc38dec56a1#file-parser-java-L16

 

in line 14 you get the item at pos I which looks correct

at line 16 you go insed that object and access index I of the legs array

i bet you want to get the whole array and not just 1 entity

just post an example input and i might can try to help you

 

just a few hints

i would use "better" function names atleast "parse" instead of prs

and i would maybe try to split the functions in sub functions

 

 

for example 1 class that parses the String s

then you put each for loop in a sub function

 

that way you can easier debug & test your code

 

if you can use libraries i would use Gson to convert json-strings to classes

if you change your entity you just have to change the annotations of that entity class

theres no need for changing "parser" functions because you dont have to write them ?

 

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Thx fot your answer!

So that's my JSON file ->

https://gist.github.com/Biseamon/dad9e9c5b91b68d4caebcfa8af7a7eb8

 

I have done some changes in the code and seems that it works, however I don't know how to test it and see if it is not repeating its values. Also, it displays the poly lines on the map but only as straight lines, which is weird. ->

 

https://gist.github.com/Biseamon/ef5e4ff015ff5cec003f81e35be973c0

 

If you could show me how would you do your way I would highly appreciate that. You can show me using libraries, even though I am not familiar with them.

 

As per the name of the parser "prs", that is just for myself. Eventually I will change to something more comprehensive.

Link to comment
Share on other sites

Link to post
Share on other sites

        Gson gson = new Gson();
        FileReader fileReader = new FileReader("C:\\Users\\KNG HOLDY\\Desktop\\directions.json");
        PolylineOptions polylineOptions = gson.fromJson(fileReader, PolylineOptions.class);
        System.out.println( polylineOptions.toString());

you didnt post your Entity so i had to mock it so dunno if this is the result you want

but this worked for me

 

the only important thing for you to know is that the class you want to serialize/deserialize (entity to text or text to entity) and all the classes it uses need to have getter setter function for each field you want to save

 

 

this is the "code" that i wrote

https://pastebin.com/raw/rU7MKUaA

all classes where generated by https://quicktype.io/ you can just copy json and it will try to generate the entity for you (java, c++, c#, ...)

i just added getter / setter / toString methods to each class

if i where you i would try to get the entity you are looking for from github because the entity i used looked clutterd af, and if the entity you are using is part of google maps than you should find it somewhere online in a clean version

(aannnddd.. obv. classes shouldnt be private & all in one class)

 

i used Gson via Maven to Convert POJO to String and Vice Versa 

https://mvnrepository.com/artifact/com.google.code.gson/gson

https://github.com/google/gson

 

i dont know what you are trying to do

if you are writing and android app you prop now about gradle, then you should use gradle instead

 

 

 

 

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×