I am working with Android Studio, andI have a string variable, called sResponse (below). According to the debugger, it holds the following value:
{
"responseData": {
"emotion":"",
"lastinput":{actionResult={"value":{"label":"green","key":"1"},"result":"success","action":"displayClickableList"},
"answer":"Sorry, I did not understand.",
"link": {
"href":"",
"target":""
},
"extraData": {
},
"responseSession": {
"id":"c4a5ef257851a991eb32c69132c9",
"transaction":"4"
},
"responseDetails": "null",
"responseStatus": "200",
"applicationUrl": "http://noki-dev.cloud.com:90/moto-1/"
}
}
When I try to initialize a JSONObject with it with in this way:
jResponse=new JSONObject(sResponse);
...The following exception rises in my Logcat:
>>>>>>>>>Thread EXCEPTION1: Response with invalid JSON format: , FrontendActivity.java L:421 ***** *org.json.JSONException: Unterminated object at character 502 of : sResponse
I suspect that those // in the URL are causing trouble. I am no expert in escaping JSON Characters. How can I obtain a valid JSONObject from the previous string? What trouble can you spot in my approach?
Problem caused because of = sign near by actionResult as well as actionResult not surrounded with double quotes and you didn't close json string properly.
Replace Json String With:
{
"responseData": {
"emotion":"",
"lastinput":{"actionResult":{"value":{"label":"green","key":"1"},"result":"success","action":"displayClickableList"},
"answer":"Sorry, I did not understand.",
"link": {
"href":"",
"target":""
},
"extraData": {
},
"responseSession": {
"id":"c4a5ef257851a991eb32c69132c9",
"transaction":"4"
},
"responseDetails": "null",
"responseStatus": "200",
"applicationUrl": "http://noki-dev.cloud.com:90/role-va-1/"
}
}
}
and add } at the end of the string.
You can track the error using following online tool:
http://json.parser.online.fr/
You missed last closing curly at the end of the response.
Just add } on last line.
Corrected json Response
{
"responseData": {
"emotion": "",
"lastinput": {
actionResult: {
"value": {
"label": "green",
"key": "1"
},
"result": "success",
"action": "displayClickableList"
},
"answer": "Sorry, I did not understand.",
"link": {
"href": "",
"target": ""
},
"extraData": {
},
"responseSession": {
"id": "c4a5ef257851a991eb32c69132c9",
"transaction": "4"
},
"responseDetails": "null",
"responseStatus": "200",
"applicationUrl": "http://noki-dev.cloud.com:90/moto-1/"
}
}
}
Related
I can't figure out how I would do the ff.
I have the ff. Payload
{
"code": null,
"message": null,
"recCtrlOut": {
},
"acctSumm": [
{
"acctBasic": {
"acctType": "SV",
"acctId": "123",
},
"acctBasic": {
"acctType": "SV",
"acctId": "321",
}
}
]
}
And I just want to get the acctId params and assign it to a new plain array of accountIds. How do I do it in Spring/Java?. Thanks
Try using json path. Library can be found here. E.g. say you had json like this:
{
"code": null,
"message": null,
"recCtrlOut": {
},
"acctSumm": [
{
"acctBasic": {
"acctType": "SV",
"acctId": "123"
}
},
{
"acctBasic": {
"acctType": "SV",
"acctId": "321"
}
}
]
}
Actual code would be something like:
List<String> ids = JsonPath.read(json, "$.acctSumm[*].acctBasic.acctId");
The above list will now hold:
["123","321"]
If you wanna learn json path syntax, you could try using this online tool. Here is also a guide to help you get started with json path.
I am trying to call an API using Retrofit in android. For this API I need the input parameter JSON object in proper sequence.
Only if I have the json in required sequence I get successful response from the API, otherwise it gives an error. To handle this issue I got one solution that is to first create a LinkedHashMap of input parameter then create the JSON of that LinkedHashMap. This way I'm acheaving the response from api.
but right now im confused how to create linkedHash map for below json
{
"RequestXml": {
"Authenticate": {
"InterfaceCode": "1",
"InterfaceAuthKey": "AirticketOnlineWebSite",
"AgentCode": "MOS0000001",
"Password": "KGBW5P"
},
"BookTicketRequest": {
"TrackNo": "0$182967|4|1AO",
"MobileNo": "9099776464",
"AltMobileNo": "9898989898",
"Email": "abc#gmail.com",
"Address": "Test",
"ClientRequestID": "",
"Passengers": {
"Passenger": [
{
"PaxSeqNo": "1",
"Title": "Mr",
"FirstName": "Savan",
"LastName": "Test",
"PassengerType": "A",
"DateOfBirth": "01/12/1992",
"PassportNo": "RTTTTGGBGB56356",
"PassportExpDate": "01/12/2024",
"PassportIssuingCountry": "IND",
"NationalityCountry": "IND"
}
]
},
"Segments": {
"Segment": [
{
"TrackNo":"0$182967|4|1AO",
"SegmentSeqNo": "1",
"AirlineCode": "UK",
"FlightNo": "888",
"FromAirportCode": "BOM",
"ToAirportCode": "DEL",
"DepDate": "30/09/2019",
"DepTime": "14:00",
"ArrDate": "30/09/2019",
"ArrTime": "16:00",
"FlightClass": "E",
"MainClass": "Y"
}
]
},
"AdditionalServices": {
},
"TotalAmount": "4735",
"MerchantCode": "PAY9zJhspxq7m",
"MerchantKey": "eSpbcYMkPoZYFPcE8FnZ",
"SaltKey": "WHJIIcNjVXaZj03TnDme",
"IsTicketing": "Yes"
}
}
}
I started working with MongoDB. I prepared some basic training JSON:
{
"header": {
"Hotel": {
"data": [
{
"name": "Hilton",
"Id": "1231213421"
}
]
},
"standard": "5",
"priceStage": "4"
},
"data": {
"http": {
"strean": {}
}
}
}
and I wrote a query like this:
db.hotel.find( "data": { Id: "1231213421"})
Why query does not return anything?
You're trying to match on an element within an array and you don't need to match on the entire array. So,something like the following will work:
db.hotel.find({"header.Hotel.data": {"$elemMatch": {"Id": "1231213421"}}} );
I need to iterate and get the last values like name, url and color from below JSON response. Using java/gson api. Please help me on this.
{
"Title": {
"desc": [
{
"name": "PRE_DB",
"url": "http://jenkins.example.com/job/my_first_job/",
"color": "blue_anime"
},
{
"name": "SDD_Seller_Dashboard",
"url": "http://jenkins.example.com/job/my_second_job/",
"color": "blue_anime"
}
]
}
}
example output :
name : SDD_Seller_Dashboard
color :blue_anime
JSONObject data = new JSONObject(your_JSON_Repsonse);
JSONArray data_desc=data.getJSONArray(desc);
for(int i=0;i<=data_desc.length();i++)
{
name=data_desc.getString("name");
url=data_desc.getString("url");
color=data_desc.getString("color");
}
I am working on a project with a SonicWall router. The responses that I get from it are in json format. I have no problems parsing them etc, etc but it seems that there is one case where the SW will return an invalid json as a response. Here is an example:
{
"success": false,
"reboot_required": false,
"status": [
{
"cli": [
{ "command": [ { "token": "no" }, { "token": "nat-policy" }, { "token": "id", "error": true }, { "token": "10", "error": true } ] },
{ "command": [ { "token": "end" } ] }
],
"info": [
{ "type": "error", "code": "CLI_E_NOT_FOUND", "message": "Nat Policy not found.
" }
]
}
]
}
Notice that the message does not close properly but changes a line? This causes the following parsingException:
Exception in thread "main" javax.json.stream.JsonParsingException: Unexpected char 13 at (line no=11, column no=97, offset=447)
at org.glassfish.json.JsonTokenizer.unexpectedChar(JsonTokenizer.java:532)
at org.glassfish.json.JsonTokenizer.readString(JsonTokenizer.java:189)
at org.glassfish.json.JsonTokenizer.nextToken(JsonTokenizer.java:376)
at org.glassfish.json.JsonParserImpl$ObjectContext.getNextEvent(JsonParserImpl.java:261)
at org.glassfish.json.JsonParserImpl$StateIterator.next(JsonParserImpl.java:172)
at org.glassfish.json.JsonParserImpl.next(JsonParserImpl.java:149)
at org.glassfish.json.JsonReaderImpl.readObject(JsonReaderImpl.java:177)
at org.glassfish.json.JsonReaderImpl.readArray(JsonReaderImpl.java:143)
at org.glassfish.json.JsonReaderImpl.readObject(JsonReaderImpl.java:180)
at org.glassfish.json.JsonReaderImpl.readArray(JsonReaderImpl.java:143)
at org.glassfish.json.JsonReaderImpl.readObject(JsonReaderImpl.java:180)
at org.glassfish.json.JsonReaderImpl.readObject(JsonReaderImpl.java:103)
Is there any way to turn this in a valid json?
Get the json response and replace all new lines first before parsing it to object.
response.replaceAll("\r?\n", "");
Sample code using GSON API
String json = "{\"msg\" : \"Hello \n World\"}";
System.out.println(json);
json = json.replaceAll("\r?\n", "");
Map<String, String> map = new Gson().fromJson(json, new TypeToken<Map<String, String>>(){}.getType());
System.out.println("Actual message:" + map.get("msg"));
Output:
{"msg" : " Hello
World"}
Actual message: Hello World