I am using version 2.4.0 of the com.jayway.jsonpath json-path library. I have attempting to parse an invalid document, but I done't get an exception thrown. The code is like
this.json= Configuration.defaultConfiguration().jsonProvider().parse(document);
The value of the document variable passed into the parse method is "notvalidjson". However the call returns fine assigning the string "notvalidjson" to the member json Object. Is this expected behaviour> I was expecting an InvalidJsonException to be thrown.
Thanks,
Paul
Managed to fix this rather than using JSONArray res = JsonPath.read(document, selector); I have used JSONArray res = JsonPath.parse(document).read(selector, JSONArray.class)
Related
I'm using this code in Java:
JSONObject jsonObject = new JSONObject(response.body().string());
and it will lead to null when I put strings like
[{...},{...}, ..., {...}]
where {...} is a valid JsonObject.
What should I do? I guess json must begin with { and end with } always. That's because it's null. What should I do? Is there a way to make JSON library deal with this automatically? I can't control the place from where I receive this string of 'json array'.
Because the string you've given isn't an object, but an array.
You'll have to read it like that (using Java EE's JSON libraries):
JsonArray array = jsonReader.readArray();
I'm trying to parse a JSON string, but getting an error when trying to get a nested object:
JSONObject jsonObject = new JSONObject(jsonString);
System.out.println(jsonObject);
System.out.println(jsonObject.keySet());
System.out.println(jsonObject.getJSONObject("matches"));
Below is the output in console. As far as I can see, the JSON is valid as jsonObject is created without an error. But when I try to obtain "matches" it throws an error. I've compared my code with tutorials but I can't see what the issue out to be:
{"matches":[{"id":233028,"awayTeam":{...
[matches, count, filters, competition]
Error in client: JSONObject["matches"] is not a JSONObject.
Anything I'm doing wrong? Happy to provide any further info if needed.
matches is an array, not an object. Use getJSONArray:
System.out.println(jsonObject.getJSONArray("matches"));
(Or more usefully:
System.out.println(Arrays.deepToString(jsonObject.getJSONArray("matches")));
since System.out.println on an array doesn't really show useful information on its own.)
There are many threads related to this, but I can't solve my issue.
I get this string from parsing an iterable using GSON.
Iterable<ParametrosProveedores> proveedoresList;
proveedoresList = proveedoresRepository.findAll(); //From spring repository
String jsonString = gson.toJson(proveedoresList);
jsonString value is:
[{\"id\":1,\"proveedor\":\"CALIXTA\",\"unaVia\":true,\"dosVias\":true,\"plazasSi\":\"todas\",\"plazasNo\":\"\",\"turnoUnaVia\":false,\"turnoDosVias\":false},{\"id\":2,\"proveedor\":\"MOVILE\",\"unaVia\":true,\"dosVias\":true,\"plazasSi\":\"51,52\",\"plazasNo\":\"\",\"turnoUnaVia\":false,\"turnoDosVias\":false},{\"id\":3,\"proveedor\":\"TWILIO\",\"unaVia\":true,\"dosVias\":true,\"plazasSi\":\"todas\",\"plazasNo\":\"51\",\"turnoUnaVia\":false,\"turnoDosVias\":false},{\"id\":4,\"proveedor\":\"OTRO\",\"unaVia\":true,\"dosVias\":true,\"plazasSi\":\"todas\",\"plazasNo\":\"\",\"turnoUnaVia\":false,\"turnoDosVias\":false}]
Which is a json array. Is there really no way to parse from that string without removing escapes manually?
All I want to do is:
JSONArray jsonArray = parseFrom(jsonString);
Is it possible?
Since you are using a generic in the form of an Iterable<T>, you may need to use:
String jsonString = gson.toJson(proveedoresList, typeOfSrc);
Where typeOfSrc is the type of your proveedoresList. that way gson knows how to serialize the object properly.
I am trying to extract list of string from following Json
"example":{[
{"#name":"name1"},
{"#name":"name2"},
{"#name":"name3"},
{"#name":"name4"}
]}
I want to get the list of #name values. I am using jayway jsonpath to do this.
I tried the following code,
List<String> response = null;
try{
ReadContext ctx = JsonPath.parse(data);
response = ctx.read(xPath);
}catch(Exception e){
AppLogger.EventLogger.error(e.getMessage());
response = null;
}
return response;
but got an exception saying could not find the specified path. I have used the following jsonPath example.#name
Can someone tell me What went wrong in my code? how to extract list #name values.
Expected output :
[name1,name2,name3,name4]
I have made small changes to your json in order to make it valid.
{"example":[
{"#name":"name1"},
{"#name":"name2"},
{"#name":"name3"},
{"#name":"name4"}
]}
and the code which specified works perfectly fine with the following expression:
$.example[*].#name
There are a lot of online services which can help validate your json.
One of them is jsonlint.com
I would also recommend to look at json.org.
I used json_encode(); to convert string to json in php and then response it to android but I can't use the response, how can I convert the json to string?
when I display the response it shows this :
"{\n'OK': \n[\n{\n'Name': 'MyName',\n'Gender':'Male'\n}\n]\n}"
what shall I do?
thank you
Since you're just converting a string to json, you're not returning a JSONObject or JSONArray, according to: http://php.net/manual/en/function.json-decode.php
If you must return a string, you may have to use some json library or write your own parser.
If that doesn't sound appealing, I recommending returning a JSONObject or JSONArray with one element.
For example:
php
echo json_encode( array('result' => 'the string you are encoding') );
java
JSONObject json = new JSONObject( encodedStringResponseFromPhp );
String theStringYouEncoded = (String) json.get( "result" );
You'll need to add a throws JSONException to the function you add this java code too or put it inside a try catch block.
Have you tried using a JSON-Library like https://code.google.com/p/json-simple/? Looks like you need some help decoding the string.
Edit: You should use the json2.js library from Douglas Crockford. It provides some extra features and better/older browser support.
Read more...