Unexpected character encountered while parsing value: c. Path '', line 0, position 0. - c#-4.0

I know the question is asked before but i tried all the options and could not get ride of it . my code is something like this
WebClient client = new WebClient();
string url = "https://demos.telerik.com/kendo-ui/service/StockData";
string EncryptedJson = client.DownloadString(url);
var dataresponse = (JObject)JsonConvert.DeserializeObject(EncryptedJson);
and at deserialzeObject it is throwing the exception
Unexpected character encountered while parsing value: c. Path '', line
0, position 0.
while the client.DownloadString(url) is giving data in the following format
callback([{"Date":"/Date(1196467200000)/","Close":40.635,"Volume":1650185491,"Open":40.640,"High":40.680,"Low":39.090}])

That's not JSON, it's JSONP.
Remove the callback( at the start and the ) at the end to make it JSON:
EncryptedJson = EncryptedJson.Substring(9, EncryptedJson.Length - 10);

Related

NodeJS using single quotes instead of double quotes in object [Answered]

[Answered by: Loulou BadWeed]
Hi #NickP. , did you stringify/serialize your payload before passing
it in the fetch body ? Does not seem the case from your example and I
don't think fetch does that for you. Try JSON.stringify(config) in the
body argument of the fetch function
I have a "small" problem.
I can't figure out, why nodejs uses single quotes for the string value instead of double quotes...
My Code
let config = {}
config["active"] = 1
config["domain"] = req.body.domain
config["mailboxes"] = req.body.mailboxes
config["defquota"] = req.body.defquota
config["maxquota"] = req.body.maxquota
config["quota"] = req.body.quota
config["restart_sogo"] = 10
console.log(req.body.domain)
console.log(config)
What I input:
{
"domain": "domain.tld",
"mailboxes": 10,
"defquota": 512,
"maxquota": 1024,
"quota": 10240
}
What I get:
{
active: 1,
domain: 'domain.tld',
mailboxes: 10,
defquota: 512,
maxquota: 1024,
quota: 10240,
restart_sogo: 10
}
The problem I have with this, is that the mailcow api doesn't like single quotes in the request body. :) Does anyone know, why this happens and can help me?
I tried replacing the single quotes with str.replace() or str.replaceAll(). But that didn't work. I tried using both types where I used the '' outside of the "".
I also already asked ChatGPT for help, but the results were just the replace and replaceAll...
PS: It's 4:30 AM when I wrote this. I'm not at 100% of my usual capacity. So please don't hate me for my question.

Nim: Can't access fields of object returned by a procedure

I was trying to write a lexer in Nim. Sorry, if this sounds a bit idiotic because I started using nim yesterday, so my problem is that I created a type such as what follows
import position
type
Error* = object
pos_start : position.Position
pos_end : position.Position
name: string
details: string
then I went on to create a procedure that returns an instance of this type,
proc IllegalCharacterError*(pos_start : position.Position, pos_end : position.Position, details : string) : Error =
return Error(pos_start: pos_start, pos_end: pos_end, name: "IllegalCharacterError", details: details)
Now, everything works fine except when I, from another module, try to access fields of this returned instance I get error
from errors import nil
from position import nil
var current_char = "2"
let pos = position.Position(idx: -1, ln: 0, col: -1, fn: fn, ftxt: text)
let error = errors.IllegalCharacterError(pos, pos, current_char)
echo error.name
The last line is the one that throws the error and following is the error that appeared during compilation
Error: undeclared field: 'name' for type errors.Error [declared in C:\Users\Mlogix\Desktop\testNim\errors.nim(4, 3)]
Thanks, any help would be really appreciated.
Ok, finally after an hour I realized that my fields weren't public. For anyone from the future, I changed my type code to
import position
type
Error* = object
pos_start* : position.Position
pos_end* : position.Position
name*: string
details*: string
and it worked. Hooray.

How to remove quotes in suds-py3 requests

I´m using suds-py3 to make requests to a service with complex arguments that requires numbers and texts. This is the code i´m trying:
from suds.client import Client
url = <service_url>
parameter = <service_method_parameter>
method = <service_method>
client = Client(url)
parameter_object = client.factory.create(str(parameter))
Until here, i´ve created a dictionary of the values that the service asks that looks like this:
parameter_object = {
'codigoAmbiente': '',
'codigoModalidad': '',
'codigoPuntoVenta': '',
'codigoSistema': '',
'codigoSucursal': '',
'cuis': '',
'nit': ''
}
So to make the request, i create another dictionary with the required values:
request_dictionary = {
'codigoAmbiente': 2,
'codigoModalidad': 2,
'codigoPuntoVenta': 0,
'codigoSistema': 'AA5BB4CC3',
'codigoSucursal': 0,
'cuis': 'A1B2C3',
'nit': 12343456
}
Then i send the values to the service with the following code.
for var, value in request_dictionary.items():
parameter_object[str(var)] = value
request_response = getattr(client.service, method)()
print(request_response)
The problem is that the service sends back an error because of the quotes, it doesn´t recognise them. If i try to send a dictionary like the following:
request_dictionary = {
'codigoAmbiente': 2,
'codigoModalidad': 2,
'codigoPuntoVenta': 0,
'codigoSistema': AA5BB4CC3,
'codigoSucursal': 0,
'cuis': A1B2C3,
'nit': 12343456
}
Python throws the error "NameError: AA5BB4CC3 is not defined".
How should my code look like so the request to the service is as intended?
I´ve made a mistake in my code. It wasn´t the quotes. The correct code is the following:
from suds.client import Client
url = <service_url>
parameter = <service_method_parameter>
method = <service_method>
client = Client(url)
parameter_object = client.factory.create(str(parameter))
for var, value in request_dictionary.items():
parameter_object[str(var)] = value
request_response = getattr(client.service, method)(parameter_object)
print(request_response)
As you can notice, the mistake was in this line:
request_response = getattr(client.service, method)(parameter_object)
in wich the "parameter_object" dicitonary that contains the request values was missing.

New style format specifier throwing tokenization error

I have the below string in the variable jss.
jss = '''
Java.perform(
function()
{
var item = Java.use("java.util.Random");
console.log("HOOKING random");
item.nextInt.overload("int").implementation = function(a)
{
var ret = this.nextInt(a);
return {0};
}
}
);
'''.format("1234")
and I am trying to use a format specifier to simply pass it some value ("1234" in this case).
When I try running this in iPython, I get the following error:
ERROR:root:An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line string', (1, 0))
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-1-a900f760c449> in <module>()
12 }
13 );
---> 14 '''.format("1234")
KeyError: '\n var item = Java'
Not sure what is going wrong here. Can someone please help understand?
you need to escape your other braces with {{ and }}- otherwise the python format tokenizer wants to make sense of the pattern. (only tested in python3...)
jss = '''
Java.perform(
function()
{{
var item = Java.use("java.util.Random");
console.log("HOOKING random");
item.nextInt.overload("int").implementation = function(a)
{{
var ret = this.nextInt(a);
return {0};
}}
}}
);
'''.format("1234")
assuming the braces you do not want to use as formatting options are always preceeded by more than one whitespace (and the format braces are preceeded by one whitespace at most) you could replace those:
import re
# with your original jss
sanitized = re.sub('(\s\s+){', r'\1{{', jss)
sanitized = re.sub('(\s\s+)}', r'\1}}', sanitized)
print(sanitized.format("1234"))
...there may be much more sensible regular expressions to suit your needs...

Groovy split using file.seperator

I'm getting an error as follows
Groovy script throws an exception of type class
java.util.regex.PatternSyntaxException with message =
Unexpected internal error near index 1
\
^
from the Split statement as follows:
String strClassPath = System.getProperty("java.class.path");
String[] path = strClassPath.split(System.getProperty("file.separator"));
How should I make this work correctly for both UNIX and Windows systems (that's why I'm using "file.separator")
Many thanks in advance
This calls java's split(String regexp). So your input must be a regexp (or must be quoted):
import java.util.regex.Pattern
def cp = {path, sep ->
path.split(Pattern.quote(sep))
}
assert cp('C:\\window\\something\\groovy.jar', '\\') == ['C:', 'window', 'something', 'groovy.jar']
assert cp('/usr/local/share/groovy.jar', '/') == ['', 'usr', 'local', 'share', 'groovy.jar']
So much for the regexp/split. If you are after the path, you might be better off using Path. e.g.
assert new File('/usr/local/share/groovy.jar').toPath().collect()*.toString() == ['usr', 'local', 'share', 'groovy.jar']

Resources