• 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

Parsing a JSON String Issues

 
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to know how to read all the values without creating a class for each pair.

This is the value being returned from the URL request.
{"sernum": "000441530","status": "1","A-B": "3.4","A-C": "3.5","A-D": "3.7","A-E": "4.1","A-F": "4.5","A-G": "5.6","GS1": "0.11","GS2": "0.15","aislelight": "Pass"}

My java only returns the value for status:

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd say to simply define more members in the results class to match the JSON keys, but because you have keys such as "A-B" which aren't valid identifiers, that's a problem.

Why do you have these keys? Are they defined elsewhere, or do you have control over them?
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:I'd say to simply define more members in the results class to match the JSON keys, but because you have keys such as "A-B" which aren't valid identifiers, that's a problem.

Why do you have these keys? Are they defined elsewhere, or do you have control over them?



Thanks, Bear.

So should I change the results class from a string to list(array)?

They are defined on a Lotus Notes database. I have asked the programmer that is creating my return string to change the 'dashes' to 'underscore'.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:So should I change the results class from a string to list(array)?


I really have no idea how you got to that from what I posted.

Once the keys are valid identifiers, you just need to add members to the class in addition to status to receive the other properties.

Naming things so that Gson can apply its automatic mapping rules is the easiest way to go. Gson allows you to create custom mappers, but you really don't want to go there unless you absolutely have to.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. ReturnedResult is a horrible name for a class. What is it actually representing?

P.P.S. You probably should also make sure that it follows all bean rules to make sure things happen correctly.
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

Steve Dyke wrote:So should I change the results class from a string to list(array)?


I really have no idea how you got to that from what I posted.

Once the keys are valid identifiers, you just need to add members to the class in addition to status to receive the other properties.

Naming things so that Gson can apply its automatic mapping rules is the easiest way to go. Gson allows you to create custom mappers, but you really don't want to go there unless you absolutely have to.



First, thanks for your help in this.

I understand how to add members to the class but not how to return multiple values.

Here is my class(String is what is returned by url)

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "return multiple values"?
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:What do you mean by "return multiple values"?



Currently it returns the status value but how can I get this same class to return the status and A_B values for example?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the toString() method? I'm not sure what that has to do with anything. Are you wanting toString() to return the JSON serialization of the class?
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:From the toString() method? I'm not sure what that has to do with anything. Are you wanting toString() to return the JSON serialization of the class?



I am not sure either. I got this whole example from the net. It worked great for one single value but now I need to get several values.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:It worked great for one single value but now I need to get several values.


From what? You still haven't explained that.

 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

Steve Dyke wrote:It worked great for one single value but now I need to get several values.


From what? You still haven't explained that.



This code returns the following:

{"sernum": "000441530","status": "1","A_B": "3.4","A_C": "3.5","A_D": "3.7","A_E": "4.1","A_F": "4.5","A_G": "5.6","GS1": "0.11","GS2": "0.15","aislelight": "Pass"}

 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is more complicated than I thought because the return string from the url can vary as for as content returned.

I will need to be able to take the returned json string and read the left hand values(these will be column headers in report) then the right hand values for row values.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may want to read the Gson documentation regarding returning a Map if the keys vary so much that you cannot capture the results in a bean.
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You may want to read the Gson documentation regarding returning a Map if the keys vary so much that you cannot capture the results in a bean.



If I use the following:

How can I get a list of the keys that the JSO string has in it?

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that actually working? If so, you can get a list of the keys just like any other java.util.Map (⇐ see javadoc).
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Is that actually working? If so, you can get a list of the keys just like any other java.util.Map (⇐ see javadoc).



I realize my understanding is not quite up to par on this but.

I simply want to get a list of the keys in the following JSON string example:

{"sernum": "000441530","status": "1","A_B": "3.4","A_C": "3.5","A_D": "3.7","A_E": "4.1","A_F": "4.5","A_G": "5.6","GS1": "0.11","GS2": "0.15","aislelight": "Pass"}

What is the most straight forward way.

Thanks again for all the awesome help.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought we already established that:
  • Use Gson (or similar) to deserialize the JSON into a Map.
  • Use the Map API to obtain the set of keys.


  •  
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    P.S. If the deserialization code you wrote doesn't work, you might need to use the following (rather non-obvious) code:



    (or something like that)
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic