JSValue myValue = ... if (myValue.isNumber()) { // We can safely perform numerical operations on this value double num = myValue.asNumber(); std::cout << "The value is a number: " << num << std::endl; } else { std::cout << "The value is not a number." << std::endl; }
JSValue myObj = JSValue::parseJSON("{\"name\": \"Bob\", \"age\": 30}"); JSValue ageValue = myObj["age"]; if (ageValue.isNumber()) { double age = ageValue.asNumber(); std::cout << "Bob's age is " << age << "." << std::endl; }This example shows how isNumber() can be used to manipulate a JSON object. The object is first created using a JSON string, and then the age value is extracted and checked if it is a number. If it is, its value is printed out. In conclusion, isNumber() is a useful method provided by the JSValue class in the JavaScriptCore library for C++.