How to get only numbers from a string - json

I have some code in json to get data from domoticz.
local Data = parsedJson["result"][1]["Data"]
print("Data:", Data)
The outcome;
2.700 kWh
How to get rid of the kWh ?
So the outcome will be 2.700

You can write a regular expression which accepts numbers and . only.
Not sure but you could try to cast the variable to float and it will preserve only the number with floating point

Related

How I will get particular value of key in this below output line in Qt?

here is my code.
QJsonObject distance = QJsonDocument::fromJson(databuffer).object();
qDebug()<<distance["feeds"];
now my output is:
QJsonValue(array, QJsonArray([{"created_at":"2021-12-06T06:11:23Z","entry_id":21,"field1":"202"}]))
i need only the value of particular "field1" key's value.
how can i get it ? please help.
Use distance.value("feeds").toArray().at(0).toObject().value("field1").
Of course you should add some error handling in case the data is not in the form as expected. But you probably want to make it more general based on your use case...
And a little explanation: QJsonValue is a general value holder and before actual use it should be converted to actual value (int, double, QString, bool etc.) or a to container QJsonArray, which can be read with position index, or object QJsonObject, which can be read by field names.

Chilkat json outputs decimal numbers with comma

In chilkat activeX (my version: 9.5.0.86), when I create object with numeric property .emit() method returns JSON string with number where decimal part is separated with comma. This depends on my regional settings
here in Control panel. But this shouldn't be dependent on this, because this JSON RFC: https://www.ietf.org/rfc/rfc4627.txt says that numbers must always be separated by dot.
This code (in VFP)
oJson = CreateObject('Chilkat_9_5_0.JsonObject')
oJson.AddNumberAt(-1,"test1", 12.3)
?oJson.Emit()
when I have set comma in settings, output of this code is:
{"test1":12,3}
and when I have set dot, output is:
{"test1":12.3}
Is there a way how to setup chilkat, to always get numbers with dot?
It seems, that problem is with method AddNumberAt, because when I get json with load method
The 2nd argument to UpdateNumber should be a string.
See https://chilkatsoft.com/refdoc/xChilkatJsonObjectRef.html#method81
You passed a floating point number, which was coerced into a string by your programming language, and the coercion is what caused the comma to be used.

Extract group of characters from an object/string

I have a very long string of characters that contain a date and time. How can I extract just the date part and the time part from it?
Here's the string:
0:2019010309130000:0.000000:126:0
I just need to extract the date and the time as in:
Date - 20190103
Time - 0913 and format it as 09:13
This applies to Python programming language.:
If the string stays in the same format I believe you should be able to extract it using a concept known as "slicing" (you can find more about it in here).
x = "0:2019010309130000:0.000000:126:0"
y = 'Date - {} Time - {}:{}'.format(x[2:10], x[10:12], x[12:14])
print(y)

json string formatting integer values

I'm trying to understand a simple basic concept regarding JSON strings. I'm running a simple test that looks like this:
$(document).ready(function() {
var last = 9;
var json1 = $.parseJSON('{"id":"10"}');
var json2 = $.parseJSON('{"id":10}');
if(json1.id > last)
alert("json1.id is greater than last");
if(json2.id > last)
alert("json2.id is greater than last");
});
Since the variable "last" is type int I'm trying to make a comparison between it and the "id" from two different JSON strings. json1 denotes the ten value as a string, whereas json2 denotes it as an integer value. When this is run, both alerts are executed. I did not expect that. I expected that the second alert would execute, but not the first one since ten is presented as a string.
I believe that the correct way to format an integer value in JSON is in json2, right?
Why is the first test executing the alert?
I'm trying to troubleshoot a larger project and thought the problem might be in the way the JSON string is formatted.
The documentation of Javascript's operators holds all the answers:
Strings are compared based on standard lexicographical ordering, using
Unicode values. In most cases, if the two operands are not of the same
type, JavaScript attempts to convert them to an appropriate type for
the comparison. This behavior generally results in comparing the
operands numerically. The sole exceptions to type conversion within
comparisons involve the === and !== operators, which perform strict
equality and inequality comparisons. These operators do not attempt to
convert the operands to compatible types before checking equality.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Comparison_operators

Action Script string to number

I have a problem with following statement
trace(Number("1/2")) //output NaN
but
trace(Number("1.2")) //output 1.2
So, I am bit confused as why the first statement doesn't gives correct result?
It probably expects the value to be a number already, not a calculation. Try to parse this string: "1+2". It'll most likely result in NaN as well.
Edit: I've run a test
Number("1.2") = 1.2
Number("1+2") = NaN
Number("1/2") = NaN
So, as I said, the Number() constructor expects a number, not a calculation.
You can convert strings that are made up of numerical characters into actual Number data using the Number(). The way it works is that you pass the String value to the Number(), and in turn, this will create a Number version of the String that was passed to it.
trace(Number("1")/Number("2")); // Output 0.5
NaN is the output because you are trying to convert the String data to be used as Number data.
You have to trace like this because "/" operator is not a number.
You can only multiply or divide numbers, NOT strings. So in the act of trying to divide String data, we are implicitly coercing the values to change into Number data. We can't do that. We should explicitly convert the String data to Number data first, and then perform the arithmetic operation.
By enclosing the value inside quotation parks you are making it an explicit string.
It's like asking what is the number value of the word "this".
Not sure if this helps but remove the quotation marks and it makes sense.
trace(Number(1/2)); //output 0.5