• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Change a Json string programmatically.

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following Json string which i'm suppose to deserialize using Gson.



My issue is: i can't modify the raw string because it is coming from a server.
The string as it is can't be deserialized because it is not well formated. If you look at the grafs key from the first subarea you can see it is an object and the grafs key from the second subarea is an array.
How can i programmatically change the string so i end up with both grafs key as an array. I think i should use string methods but i don't know how.
Please help me with this.
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you actually tried to parse it into Java Objects with gson? The json looks well formed, just not consistent in structure.
 
Rui Sousa
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes i did, and it is throwing me an error when parsing, because since i have a Grafs class which is a list of rows, as you can see below:



When parsing, when it reaches the first rows it throws an error because it expects an array but it gets an object.
That's why i need to uniformize the Json before parsing it.
Below are the other classes for parsing.

Example.java



Result.java



Row.java



Row_.java



Rows.java



Subarea.java



And finally my main method:



The newJson string is the one i showed above in the first post.
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The right thing to do here is to ask the providers of that json data to fix up their data structure. Can you do that? Or know someone who can do that?
 
Rui Sousa
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually i did that but since the Json comes from xml, they don't know when it is an array or an object. I need a way to uniformize the string programmatically.
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem lies in the conversion between xml and json. The program that does that part has a defect and I suggest you log that with the relevant team. Anything you do to accommodate it will be a nasty hack.

The easiest way I can think of to parse the json is to write a Java Object that conforms to the data structure you have and then edit your Java data structure afterwards.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then, always make it an array which is the more general form.

Both sides need to agree on the structure, but if you don't have a strong definition of that structure (such as, "well, sometimes it's an array and sometimes not), then all is lost.

Create a strong definition of the structure, and make sure that both ends stick to it.
 
Rui Sousa
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what i did but it throws an error when it reaches the json string first Grafs key, since is an object. I need to fix the Json string programmatically before deserializing it using GSON. That's my issue, that's what i need help doing? Can you help me doing that?
 
Rui Sousa
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, myJSON should have Symmetry (See "grafs" key under "subareas" key) - In first value it is as -



And in second value it is as -



i need to make it like this programmatically.

 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That last post of yours should be the basis of the defect report you submit against the application producing the json data.

In the meantime, instead of getting gson to parse directly into custom Java Objects you could parse it into regular Java collections, Lists and Maps, and then convert that into your custom Objects yourself. Somewhere you'll have a Map with key "grafs" and the value will either be a Map or a List which you can then deal with as required.
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My apologies, that's not quite right.

You'll have a Map with a key of "grafs", who's value is a Map with a key of "rows", who's value will be a Map or a List.
 
Rui Sousa
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, i got your point but since i'm new to Gson, can you, please show me how to do it, or point me to any resource that helps doing that? I really need to get this done.
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd start with the gson github page first for guides and usage examples.

But remember, telling us you "really need to get this done" conveys an urgency that is not shared. Such language tends to have the opposite effect than you'd like, so please have patience.
 
Rui Sousa
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry for that and thanks for the advice. It is because i've been trying to get this done for 3 days and it is getting frustrating. But i won't give up on it. Any help is really appreciated, believe me. I think you know that feeling of struggling with something and it stills does not work. But as i said i wont give up.
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found parsing the entire json String as a Map seemed to be a reasonable starting point.
 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic