My requests return BigInteger id. I use these lines to extract them from response:
var data = JSON.parse(responseBody);
postman.setEnvironmentVariable("id", data.id);
I updated Postman App to version 4.9.3 and caught a problem with environmental variables.
For example, I receive id="9141989209013199260" and expect app to save "9141989209013199260", but i'm getting "9141989209013199000" instead. Any ideas?
var str = '{ "id": 9146756954251460484, "name" : "test" }';
str = str.replace(/\d{19}/, '"$&"');
var obj = JSON.parse(str);
alert(obj.id);
Done!
Related
I need to test an api endpoint where response of the endpoint will be like this
Response:
{
"items": [
{
"url": "http://www.localhost.com:8080/user?id=19909090"
}
]
}
I want to store the id value that is 19909090 to a variable. Can you please suggest some solution to achieve this ?
You can use JsonPath to read the value of url.
For example:
String url = from(json).get("$.items[0].url");
And then use java.net.URI to extract the query parameter value.
For example:
URI uri = URI.create(url);
String[] params = uri.getQuery().split("=");
// prints out 19909090
System.out.println(params[1]);
I am trying to use meteor and iOS for a project of mine. My problem is that I am trying to connect to a meteor function using the swiftDDP library. When sending data it is sent in the wrong format. How can I pass an object from iOS that Meteor (running in js) will accept? Is there a way to do this using a string? I have working javascript code that calls the same functions how would I pass the same form of data from iOS/swift?
Javascript
Meteor.call('xxx', {a: 1, b: 2, c: "some text"});
IOS (passes anyobject)
Meteor.call("xxx", ?? )
Solved this I created a object in this format on IOS swift let data: [String : AnyObject] = [ "time" : {time object} "foo" : "1", "blah" : "1", ] Then simply called Meteor.call("events.add", params: [data]) { result, error in // Do something with the method result NSLog("call worked") }
I have a Firebase instance where I want to store a dictionary of values I want to store into firebase. I have looked on the documentation https://www.firebase.com/docs/ios/guide/saving-data.html as a reference but can't seem to get it to work. The following is my attempt:
//Declared above are the currentUser values as so:
var currentUserFirstName: String!
var currentUserLastName: String!
var currentUserObjectID: String!
var attendeesArray = ["objectID": currentUserObjectID, "name": currentUserFirstName + " " + currentUserLastName]
var eventRefChild = EventReference.childByAutoId()
eventRefChild.setValue([
"eventName":eventName.text,
"attendees": attendeesArray,
"eventCreator": currentUserFirstName
])
But I keep getting an error saying: Could not find an overload for '+' that accepts the supplied arguments when I try to do eventRefChild.setValue([... and I'm honestly not too sure why I am getting this issue. Any help would be appreciated!
EDIT: The variable EventReference is assigned as so: EventReference = Firebase(url:"<Insert Firebase URL>")
And inside currentUserFirstName and currentUserLastName are an individual's first and last name grabbed from Facebook so it would look something like Bob Smith respectively.
There is nothing wrong with your code. The issue is the values that are being loaded into
var currentUserFirstName: String!
var currentUserLastName: String!
As a test, I created a sample project with the following code, which is a duplicate of your posted code but with normal strings loaded into the var's:
var myRootRef = Firebase(url:"https://myproject.firebaseIO.com/")
var currentUserFirstName = "Test"
var currentUserLastName = "User"
var currentUserObjectID = "object ID"
var attendeesArray = ["objectID": currentUserObjectID, "name": currentUserFirstName + " " + currentUserLastName]
var eventRefChild = myRootRef.childByAutoId()
eventRefChild.setValue([
"eventName": "eventName",
"attendees": attendeesArray
])
the project compiled and runs correctly and the expected data is written to Firebase. Note that the eventName.text was replaced with a string as well but that doesn't affect the answer.
The investigation needs to turn to what's loaded in the var's, and the answer is that one of those var's, currentUserFirstName or currentUserLastName is being loaded with an OBJECT (class), not a string.
As a side note, why are the var's declared as implicitly unwrapped optionals (the !)
edit: adding additional info to deal with the optionals
if let actualString = currentUserFirstName {
println(actualString) //proceed working with the the optional string
}
else {
return // something bad happened! currentUserFirstName does not contain a string
}
To prevent code from errors when an optional contains no value, add the above code directly above the concatenation line of code. What happens here is the we are assigning the string in currentUserFirstName (optional var) to a actual string (a standard, non-optional var).
If the expression evaluates to true then we can proceed evaluating currentUserFirstName.
If it's false, then currentUserFirstName does not contain a string so handle the error elegantly.
I have a json object as below
{"level" :{"currentLevel":"1","score":"100"}}
I have this json data in my project folder and I am using SwiftyJSON to parse my son and read the values. Everything looks fine.
Now I need to update the score and I am trying as below
var json = JSON({"level" :{"currentLevel":"1","score":"100"}})
json["level"]["score"] = "200"
This works fine too and the json is updated but below try fails
var json = JSON({"level" :{"currentLevel":"1","score":"100"}})
var updatedScore:String = "200"
json["level"]["score"] = updatedScore
I get compile error
Type [Subscript] does not conform to Protocol 'StringLiteralConvertible'
Any suggestion on how to update a SwiftJSON JSON object with a variable would be helpful
Thank you
Update:My Solution
This is what I have done finally
var json = JSON({"level" :{"currentLevel":"1","score":"100"}})
var level = (json["level"] as JSON).dictionaryObject
let updatedScore = "200"
level!["currentLevel"] = updatedScore
json["level"] = JSON(level!)
And this works
Try the below if you are saving the json as dictionary
((json["level"]as nsdictionary)["score"] as NSString = updatedScore)
I am not getting the expected result from this F# code. I would expect t to contain values as a result of the call to JsonSchema.Parse(json) but instead it is empty. What am I doing wrong?
open Newtonsoft.Json
open Newtonsoft.Json.Schema
let json = """{
"Name": "Bad Boys",
"ReleaseDate": "1995-4-7T00:00:00",
"Genres": [
"Action",
"Comedy"
]
}"""
[<EntryPoint>]
let main argv =
let t = JsonSchema.Parse(json)
0 // return an integer exit code
As John Palmer points out, JsonSchema.Parse parses a JSON schema, but from your question, it looks as though you want to parse a normal JSON value. This is possible with JsonConvert.DeserializeObject:
let t = JsonConvert.DeserializeObject json
However, the signature of DeserializeObject is to return obj, so that doesn't particularly help you access the values. In order to do so, you must cast the return value to JObject:
let t = (JsonConvert.DeserializeObject json) :?> Newtonsoft.Json.Linq.JObject
let name = t.Value<string> "Name"
Json.NET is designed to take advantage of C#'s dynamic keyword, but the exact equivalent of that isn't built into F#. However, you can get a similar syntax via FSharp.Dynamic:
open EkonBenefits.FSharp.Dynamic
let t = JsonConvert.DeserializeObject json
let name = t?Name
Notice the ? before Name. Keep in mind that JSON is case-sensitive.
Now, name still isn't a string, but rather a JValue object, but you can get the string value by calling ToString() on it, but you can also use JValue's Value property, which can be handy if the value is a number instead of a string:
let jsonWithNumber = """{ "number" : 42 }"""
let t = JsonConvert.DeserializeObject jsonWithNumber
let actual = t?number?Value
Assert.Equal(42L, actual)
I recommend to use Json type provider instead.