I'm using dankogai/swift-json to get a json response from a web service..
Everything is going fine but, sometimes the web service can't give me back any response, because there is no data in the database. It's not a problem.
But I have to handle when I get "null" from the web service.
This is my json request:
let json = JSON(url:"http://79.172.249.175:7001/RestWebServiceApp/webresources/entity.bkkmainprtable/"+lat+"/"+lon)
// json is valid json object...
if (json["bkkMainPrTable"]["routeShName"].isArray){
for (k, v) in json["bkkMainPrTable"] {
colors.append(v["routeShName"].description + " - " + v["stopName"].description)
}
}
// json is not a valid json object...
else {
colors.append("Nincs elérhető járat")
sendKallerBtn.setTitle("No Post", forState: UIControlState.Normal)
}
But, it is always nil.. and alway step into else..
Can anybody help me how to check is this a valid json object or not..
Thank you!
Solved with this:
if (json["bkkMainPrTable"].asError == nil){
....
}
Sure, I can help.
I've made a call:
http://79.172.249.175:7001/RestWebServiceApp/webresources/entity.bkkmainprtable/47.490477/19.030486
got the response:
{"bkkMainPrTable":[{"id":"7857","routType":"3","routeShName":"178","stopId":"F00002","stopLat":"47.490477","stopLatStirng":"47.490477","stopLon":"19.030486","stopLonString":"19.030486","stopName":"Zsolt utca"},{"id":"7954","routType":"3","routeShName":"105","stopId":"F00087","stopLat":"47.496422","stopLatStirng":"47.496422","stopLon":"19.03071","stopLonString":"19.03071","stopName":"Krisztina tĂŠr"},{"id":"7946","routType":"0","routeShName":"18","stopId":"F00080","stopLat":"47.493779","stopLatStirng":"47.493779","stopLon":"19.038183","stopLonString":"19.038183","stopName":"DĂłzsa GyĂśrgy tĂŠr"},{"id":"7943","routType":"3","routeShName":"916","stopId":"F00077","stopLat":"47.494777","stopLatStirng":"47.494777","stopLon":"19.037665","stopLonString":"19.037665","stopName":"DĂłzsa GyĂśrgy tĂŠr"}]}
put it there:
http://jsonlint.com/
and got the result:
{
"bkkMainPrTable": [
{
"id": "7857",
"routType": "3",
"routeShName": "178",
"stopId": "F00002",
"stopLat": "47.490477",
"stopLatStirng": "47.490477",
"stopLon": "19.030486",
"stopLonString": "19.030486",
"stopName": "Zsolt utca"
},
{
"id": "7954",
"routType": "3",
"routeShName": "105",
"stopId": "F00087",
"stopLat": "47.496422",
"stopLatStirng": "47.496422",
"stopLon": "19.03071",
"stopLonString": "19.03071",
"stopName": "Krisztina tĂŠr"
},
{
"id": "7946",
"routType": "0",
"routeShName": "18",
"stopId": "F00080",
"stopLat": "47.493779",
"stopLatStirng": "47.493779",
"stopLon": "19.038183",
"stopLonString": "19.038183",
"stopName": "DĂłzsa GyĂśrgy tĂŠr"
},
{
"id": "7943",
"routType": "3",
"routeShName": "916",
"stopId": "F00077",
"stopLat": "47.494777",
"stopLatStirng": "47.494777",
"stopLon": "19.037665",
"stopLonString": "19.037665",
"stopName": "DĂłzsa GyĂśrgy tĂŠr"
}
]
}
Valid JSON
Related
I have a problem parsing a string object to JSON with Golang.
there are double quotes in some fields values.
I'm trying to use a regex pattern but it doesn't work for me, I'm doing the conversion from bigquery which is based on the Golang code but it doesn't work for me.
{
"Responses": [],
"SessionParameters(Updated)": {
"counter-no-match-nombre": "($session.params.counter-no-match, 1)",
"message": "Hello."
},
"SystemFunctionResults": {
"SystemFunctionErrors": [
{
"Message": "Wrong type of argument for function "NONE", expected 1th argument to be one of type: ["number"]."
}
]
}
}
here a example
([:[,{]\s*)"(.?)"(?=\s[:,]}])
Test regex
SELECT
REGEXP_REPLACE('{
"obj1": "value is "test" of test, \"obj1\" val",
"result": "your result is "out" in our website",
"info": {
"x": [
{
"desc": "shjjfdsajfjkfsdkf, "sfsdfsdfdsf" fjkdfgjkfd."
}
]
}
}','([:\\[,{]\\s*)"(.*?)"(?:\\s*[:,\\]}])','NONE')
AS json_data;
I would appreciate your help, thanks
I'm trying to parse json into kotlin objects but the problem is that its key fields are numbers any idea how can parse them , I've tried serialized name but still facing problem.
The json response looks like this :
{
"Id": [{
"1": {
"name": "name1",
"class": "11a"
}
},
{
"2": {
"name": "name2",
"class": "11b"
}
}
]
}
I'm using gson and the main thing i'm trying to do is to store this number fields as some other string objects.
You can parse them into a list of maps, then "map" those to your data classes instead:
val input = """{
"Id": [{
"1": {
"name": "name1",
"class": "11a"
}
},
{
"2": {
"name": "name2",
"class": "11b"
}
}
]
}"""
val gson = Gson()
val parsed: Map<String, List<Map<String, Any>>> =
gson.fromJson(input, (object : TypeToken<Map<String, List<Map<String, Any>>>>(){}).type)
println(parsed["Id"]?.get(0)?.get("1")) // {name=name1, class=11a}
It will have some nasty generic signature, though.
If you're working with Kotlin, take a look at Klaxon, it will improve your experience.
I have a JSON that I want to parse in Ruby. Ruby is completely new to me, but I have to work with it :-)
Here is my litte snippet, that should do the parsing:
response = File.read("app/helpers/example_announcement.json")
JSON.parse(response)
this works pretty fine. The only downside is, I do not know the properties at the point where I use it, it is not typesafe. So I created the objects for it
class Announcements
##announcements = Hash # a map key => value where key is string and value is type of Announcement
end
class Announcement
##name = ""
##status = ""
##rewards = Array
end
And this is how the json looks like
{
"announcements": {
"id1" : {
"name": "The Diamond Announcement",
"status": "published",
"reward": [
{
"id": "hardCurrency",
"amount": 100
}
]
},
"id2": {
"name": "The Normal Announcement",
"players": [],
"status": "published",
"reward": []
}
}
}
So I tried JSON parsing like this
response = File.read("app/helpers/example_announcement.json")
JSON.parse(response, Announcements)
But this is not how it works^^can anybody help me with this?
I have a node js program, which uses the express framework. What happens, is a POST request is made using Postman to my API, and I deal with the request as required (which works great).
However, I want send back an XML response to the API call. So doing some digging online, I have found this library - https://www.npmjs.com/package/xml
I tried to adapt it to my code, so I need to convert the following json object into an XML response:
var responseJson = [{
"methodResponse": {
"params": {
"param": {
"value": {
"struct": {
"member": [
{
"name": "myValue",
"value": {
"string": "hi"
}
}
]
}
}
}
}
}
}];
And then in the response I do the following:
res.header('Content-Type', 'text/xml');
res.send(xml(responseXml, true));
However this only returns:
<methodResponse/>
and nothing else in the Postman response.
Any idea what happened to the rest and why only one line is returned? Is there a better way to do this? Thanks
You need to put square brackets around your objects.
const data = [{
"methodResponse": [{
"params": [{
"param": [{
"value": [{
"struct": [{
"member": [{
"name": "myValue",
},{
"value": [{
"string": "hi"
}]
}]
}]
}]
}]
}]
}]
}];
Which will produce:
<methodResponse><params><param><value><struct><member><name>myValue</name><value><string>hi</string></value></member></struct></value></param></params></methodResponse>
I am using mongoose as JSON Schema and node.js with it. Need not say, I am new to both. I have been struggling through the day to get this thing work for me but couldn't. Finally, the only solution was to get help from some real nice people out here.
Here is my schema definition -
UserName = {
"properties": {
userURL: {
"description": "URL of this resource",
"type": "string"
},
userName : {
"description": "UserName",
"type": "string",
"required": true
},
}
}
When I make a get call to it, it returns the response in following format -
[
{
"_id": "54c5ede55c82c4bd6abee50a",
"__v": 0,
"properties": {
"userURL": "http://localhost:3000/54c1d6ae441ae900151a6520",
"userName ": "testUser"
}
}
]
Now my requirement is to return the response in following format -
[
{
"_id": "54c5ede55c82c4bd6abee50a",
"userURL": "http://localhost:3000/54c1d6ae441ae900151a6520",
"userName ": "testUser"
}
]
i.e without version and properties tags. I am able to get away with version using following code but properties seems to be tricky thing -
.get(function(request, response) {
UserSchemaModel.find().select('properties.userURL properties.userName').exec (function (err, resObj) {
if (err)
response.send(err);
else{
response.json(resObj);
}
});
});
But it still has properties field :( -
[
{
"_id": "54c5ede55c82c4bd6abee50a",
"properties": {
"userURL": "http://localhost:3000/54c1d6ae441ae900151a6520",
"userName ": "testUser"
}
}
]
I did some google around select as, alias name in select,population in mongoose but no luck.
Kindly suggest. With best Regards.
Just make a new object
response.json(
{
"_id": resObj["_id"],
"userURL": resObj["properties"]["userUrl"],
"userName": resObj["properties"]["userName"]
}
);
Update: Since resObj is an array (as per your comment), you can use Array.prototype.map() to transform them into the right format like so:
response.json( resObj.map(function(o){
return {
"_id": o["_id"],
"userURL": o["properties"]["userUrl"],
"userName": o["properties"]["userName"]
};
})
);
This will return a list of transformed objects that then get passed into the response.json() method.