Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Matching HTTP response with JSON file  RSS feed

 
Michael Voase
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am currently working on game automation for a Flash game of 3 Card Poker. Currently the cards are passed through as JSON with a number between 0-51 (52 cards in a deck) that are unique to a specific card.
I have retrieved the response from the game, which takes the resposne of the 3 cards for example: 50, 2, 4.

These have then been split into individual values.

I have then created a JSON file to be able to pull from the match the ID from the response to determine which card it is and which suit.

I am unsure on how to carry this out:
1. Retrieving information from the JSON file.
2. Checking which card it is based on the response retrieved.

Here is the code currently that retrieves the response:



Here is a link to the JSON file.

Cards.json

Moderator edit: here is the JSON content:
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What content is returm with response.getBody() - can you post it here?  It doesn't seem like it is what is returned by your URL.

Also, what are you trying to do with new BigDecimal(Double.valueOf(card1))?  I would expect card values to be small integer values - why are you using Double and BigDecimal types?
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parsing JSON is not something you should do yourself. There is a useful class called JSONObject made for parsing JSON. You can create it directly from a JSON string:

Then you can retrieve data from it by using the get methods. It looks like that JSON above is made up of 4 JSON Arrays, which then have the contents, so you could use:

And then use the getString methods to parse inside there.

Documentation:
http://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html
http://docs.oracle.com/javaee/7/api/javax/json/JsonArray.html
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and Welcome to the Ranch 
 
Michael Voase
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:What content is returm with response.getBody() - can you post it here?  It doesn't seem like it is what is returned by your URL.

Also, what are you trying to do with new BigDecimal(Double.valueOf(card1))?  I would expect card values to be small integer values - why are you using Double and BigDecimal types?


Firstly here is the response from response.getBody():



Then this splits the card values into:


I have then changed the code to Integer and removed Double and Big Decimal.
This returns the individual values of the cards - 13, 50, 26.

So if you view the JSON these cards are:

2 Hearts
King Clubs
2 Diamonds.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!