Jersey REST Character encoding - java

I have some problems concerning charater encoding with gson.
I have a webservice which sends following JSON response:
{
"version": "v2",
"result": {
"class": "SearchSizeBean",
"result_list": [
{
"id": 0,
"name": "Bitte w\u00c3\u00a4hlen"
},
{
"id": "21",
"name": "176"
}
]
}
}
I request the webservice with following code:
ClientResponse response = webResource.path(version)
.path(path)
.accept(MediaType.APPLICATION_JSON)
.method(method.name(), ClientResponse.class, paramMap);
if (response.getStatus() == 200)
{
responseBody = response.getEntity(String.class);
...
}
When checking responseBody the resulting string shows exactly the same result as the JSON mentioned above. But after using GSon to convert the JSON string into an Object, the characters are converted wrong:
Parsing with GSON:
Gson gson = new GsonBuilder().disableHtmlEscaping()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapter(Transferable.class, new TransferableDeserializer())
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.create();
RestResponse responseDTO = gson.fromJson(responseBody, RestResponse.class);
Resulting content:
Response: SearchSizeBean[
resultList={
PosSize [id=0, name=Bitte wählen],
PosSize [id=21, name=176]
}
]
UPDATE:
I had copied the wrong JSON content. I have edited the JSON to show the correct JSON string. The data is sent in unicode w\u00c3\u00a4. When I check the response after conversion by GSon it becomes hexadecimal: C3 82 C2 A4

The problem was on the server side php code. I have a script, which was already encoded in UTF-8. I have encoded the string "Bitte wählen" with the php function utf8_encode() which led to my problem.
Thank you McDowell, your hints brought me on the right track!
The solution was not to use the function and just send the string "Bitte wählen".

Related

Get data from outside the root level of response

I'm still kind of new to REST and haven't been able to figure this one out.
I have a response like this one:
{
"StatusCode": 200,
"Result": {
"CustomerStuff": {
"Name": "John",
"State": "Oregon",
"GetEmail": false
},
"eText": "Will only get paper mail. "
}
}
I would normally save the response body as a string and then use a JsonPath to get what I need.
String responseBody = given().body().when().etc...;
JsonPath jsonPath = new JsonPath(responseBody).setRoot("Result.CustomerStuff");
Then get what I need:
String name = jsonPath.get("name");
I can't figure out how to get the, "eText" value. It's not in the same segment of the response.
Any suggestions?
You should use
JsonPath jsonPath = new JsonPath(responseBody).setRoot("Result")
And then call jsonPath.get("eText") in order to get the value you want.
You can still access CustomerStuff with jsonPath.get("CustomerStuff")

Gson Illegal state exception from inconsistent json

I am fairly new to Java and I have tried looking up an answer for this for the last couple hours and so far I have nothing.
I am working with the Nobel Prize API for a project and I am getting an error when I try to convert to classes from the Json stream.
My code to read in the stream is this:
Gson gson = new Gson();
String sURL = "http://api.nobelprize.orgv1/laureate.json?bornCountry=Canada"; //just a string
// Connect to the URL using java's native library
URL url = new URL(sURL);
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.connect();
// Convert to a JSON object to print data
JsonParser jp = new JsonParser(); //from gson
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //Convert the input stream to a json element
JsonObject rootobj = root.getAsJsonObject(); //May be an array, may be an object.
LaureatesMain person = gson.fromJson(rootobj, LaureatesMain.class);
System.out.println(person);
}
I am not checking for errors from the stream yet.
The json stream that I am looking at looks something like this:
{
"laureates": [
{
"id": "140",
// MORE STUFF
"prizes": [
{
"year": "1990",
// MORE STUFF
"affiliations": [
{
"name": "Stanford University",
"city": "Stanford, CA",
"country": "USA"
}
]
}
]
}
]
}
I have a class for the laureates, prizes and affiliations and when it runs on Json like above I have no issue and it goes into the classes fine.
The problem I run into is if there is no entry in the affiliations then for some reason the API flips the {} to [] and gives me the following:
{
"laureates": [
{
"id": "140",
// MORE STUFF
"prizes": [
{
"year": "1990",
// MORE STUFF
"affiliations": [[]
]
}
]
}
]
}
When I run on this one I get Expected BEGIN_OBJECT but was BEGIN_ARRAY at path
$.laureates[###].prizes[0].affiliations[0]
I have tried looking at changing the GsonBuilder and tried to intercept the Json and change it before it goes into the Builder but so far nothing has worked for me.
I have no control over what I get from the API. I can deal with it being an empty class object or a null value in the final structure. This is a fairly early iteration of the project but so far this issue is stopping me from moving on to other things.
Thanks
Answering my own question so I can close it.
I ended up using a Type Adapter so thank you for directing me to that. Wasn't too hard once I knew what I was looking for.
Found really good information in the following java docs at :
https://google.github.io/gson/apidocs/com/google/gson/TypeAdapter.html
https://static.javadoc.io/com.google.code.gson/gson/2.6.2/com/google/gson/stream/JsonReader.html

Json parser error? Amazon API incorrect JSON response?

So here is the code I use to parse the amazon JSON response for my inventory of products.
ListInventorySupplyResponse response = client.listInventorySupply(request);
ResponseHeaderMetadata rhmd = response.getResponseHeaderMetadata();
String responseJSON = response.toJSON();
JSONParser parser = new JSONParser();
try {
json = (JSONObject) parser.parse(responseJSON);
It worked for a week, and then I got this error today.
Unexpected token VALUE(-1) at position 5948.
looking through the response JSON. I put it in a JSON validator (http://jsonlint.com/)
jsonlint told me that the response I was getting was not correct json! Here was the error
{
"SellerSKU": "zz",
"FNSKU": "B006T5BLTO",
"ASIN": "B006T5BLTO",
"Condition": "NewItem",
"TotalSupplyQuantity": 92,
"InStockSupplyQuantity": 44,
"EarliestAvailability": {
"TimepointType": "DateTime",
"DateTime": 2015-01-13T09: 00: 00Z
},
"SupplyDetail": {
"member": [
]
}
},
{
"SellerSKU": "yyC",
"FNSKU": "B00IHMDJ7Y",
"ASIN": "B00IHMDJ7Y",
"Condition": "NewItem",
"TotalSupplyQuantity": 63,
"InStockSupplyQuantity": 63,
"EarliestAvailability": {
"TimepointType": "Immediately"
},
"SupplyDetail": {
"member": [
]
}
},
Parse error on line 398:
... "DateTime": 2015-01-13T09: 00: 00Z
----------------------^
Expecting '}', ',', ']'
As you can see, for some reason most of products are available immediately but one is not and Amazon sends me a "datetime" for when it is. However they dont put the date time in quotes making it an invalid JSON.
Is it me? Is it them? How do I handle this. My program doesnt need this part of the JSON but I dont know how to take this field out or tell the parser to "skip over it" so that it can parse the rest of the fields minus this one incorrect field that I dont care about anyway.
Thanks!

How to read JSON object from URL query string

I have a URL request like this:
http://localhost:8080:_dc=1367504213107&filter[0][field]=site&filter[0][data][type]=string&filter[0][data][value]=test&filter[1][field]=address&filter[1][data][type]=string&filter[1][data][value]=Columbus
This is the URL request that I get from browser.
From this URL, I need to get the filter related data as a JSON object.
Basically I have filter parameters like these in the requested URL:
filter[0][field]=site
filter[0][data][type]=string
filter[0][data][value]=test
filter[1][field]=address
filter[1][data][type]=string
filter[1][data][value]=Columbus
I am using the Spring MVC framework.
Those URL parameters aren't in JSON format. I'm not sure what your question / problem is here... can't you just read in each of those parameters from the URL and then parse the data out of the strings as you need? You could take it as a single parameter and tokenize based on a custom character, or you could just loop through all the parameters and parse each one and add them to an array.
Are you saying that you need to return the data in JSON format? Get your data using the parsed parameters as I described above, then when you have the data merely serialize that object and pass it back over the wire as the body of the response. You'll want to use a JSON serialization library like Jackson which will write the object to a string in JSON for you.
This is what that data would probably look like if it were written in JSON:
{
"filter":
[
{
"field": "site",
"data":
{
"type": "string",
"value": "test"
}
},
{
"field": "address",
"data":
{
"type": "string",
"value": "Columbus"
}
}
]
}
Java code that translates from the above JSON to your-format:
JSONObject root = new JSONObject(json);
JSONArray filters = root.getJSONArray("filter");
for (int i = 0; i < filters.length(); i++)
{
JSONObject filter = filters.getJSONObject(i);
String field = filter.getString("field");
JSONObject data = filter.getJSONObject("data");
String dataType = data.getString("type");
String dataValue = data.getString("value");
System.out.println("filter[" + i + "][field]=" + field);
System.out.println("filter[" + i + "][data][type]=" + dataType);
System.out.println("filter[" + i + "][data][value]=" + dataValue);
}
Your data format does not appear to be standard, so you will have to parse it yourself.

How to parse data from JSON array coming from FB graph API?

I am new to JSON. I'm working with the json processing library. Given a JSON such as:
{
"data": [
{
"name": "John Doe",
"id": "2980311"
},
{
"name": "Jane Doe",
"id": "10221412"
},
{
"name": "George Doe",
"id": "111623489"
}
],
"paging": {
"blah" : "blah"
}
}
How can I get the id values. I've started by trying to get an array to work with:
String URL = "https://graph.facebook.com/username/friends?access_token=";
String token = "";
String[] response = loadStrings(URL+token);
if(response != null) {
JSONObject result = new JSONObject(response);
JSONArray data = result.getJSONArray("data");
}
And this prints out:
JSONObject["data"] not found.
JSONObject["data"] is not a JSONArray.
But it's definitely getting the data. If I print out response I see the data.
What am I missing?
I'm not seeing a JSONObject constructor in that library that accepts String[] (the type of your response variable). There's JSONObject(String) and there's JSONObject(Object). If you pass a String[] into JSONObject, it'll match the latter, which doesn't (immediately) look to me like it'll do what you want.
I suspect you want to get a single String from the URL, which you'd then pass into JSONObject(String), at which point things should start working correctly.
try with this..
GraphObject responseGraphObject = response.getGraphObject();
JSONObject json = responseGraphObject.getInnerJSONObject();
JSONObject sys = json.getJSONObject("data");
Log.e("urlimage json", sys.getString("url"));

Categories

Resources