I got a json through the spider, but the json format has a problem, name:value, the name is missing double quotes. like this:
{ listInfo:[{title:'it is title',url:'http://test.com',imgurl:'http://test.com',imgurl2:'',abstract:'',source:'',pubtime:'2019-05-22 11:47:24'},{title:'xx',url:'http://test.com/1.htm',imgurl:'http://test.com',imgurl2:'',abstract:'',source:'',pubtime:'2019-05-22 07:54:46'}]}
I want add double quotes in "name",and need to exclude String of [http { ...]
{ "listInfo":[{"title":'it is "title"',"url":'http://test.com',...
I tryed this but it is not work
#(.*?)\:(.*?)\n'
pattern = re.compile(r'^((?![http]\").)*\:(.*?)\n', re.MULTILINE )
content = content.replace(pattern.search(content).group(1),'\"'+pattern.search(content).group(1).strip()+'\"')
I also tryed
How to add double quotes to the dictionary?
content = '''
{ listInfo:[{title:'it is title',url:'http://test.com',
imgurl:'http://test.com',imgurl2:'',abstract:'',source:'',
pubtime:'2019-05-22 11:47:24'},{title:'xx',url:'http://test.com/1.htm',
imgurl:'http://test.com',imgurl2:'',abstract:'',source:'',pubtime:'2019-05-22 07:54:46'}]}
'''
# dict_str = lambda data : re.sub(r'(\w+):\s*(-?\d[\d/.]*)',r'"\1": "\2"',data)
dict_str = lambda data : re.sub(r'(\w+):(.*?)\n',r'"\1": "\2"',data)
for i in [content] :
var1=dict_str(i)
print(var1)
the result is look like:
{ "listInfo": "[{title:'it is title',url:'http://test.com',""imgurl": "'http://test.com',imgurl2:'',abstract:'',source:'',""pubtime": "'2019-05-22 11:47:24'},{title:'xx',url:'http://test.com/1.htm',""imgurl": "'http://test.com',imgurl2:'',abstract:'',source:'',pubtime:'2019-05-22 07:54:46'}]}"
Who knows how to write regEx.
Thinks!
I used a comparative method to solve it.
script = script.replace('abstract','\"abstract\"')
...
:(
Related
I am passing a List
errors.add(new ValidationError("Employee " + strId, "error.range," + strName +","+ intRange));
that will build a string
"Employee1","error.format,FIRST NAME,20
I want to split the message string with "," and store it in a array variable in scala template or twirl in view. I'm not really good at scala code i don't know how to store a variable in scala template nor know the syntax in splitting a string. Is there a code that can do this task in scala template?. Thank you.
<div id = "msg-menu" class = "msg-menu">
#for((key, value) <- appModel.errors) {
<div class="error-msg">
<p>#key :
#for(err <- value) {
#for(error <- err.message.split(",")) {
#Messages(error)
}
}
</p>
</div>
}
</div>
What i did was use a for loop to do it but it is not what i needed. i need to declare all the string in an array and use them as parameters on the #Messages. like:
#Messages(error[0],error[1],error[2])
In which error0 = "error.range", error1 = "FIRST NAME" and error2 = "20". In conf/message i will build an error message with the parameters
error.range = Enter {0} in {1} characters or less.
Variables are definined in twirl using #defining, in the following way:
#defining( err.message.split(",").lift ) { errors =>
...
}
The use of lift could be handy, as it helps with index out of bounds conditions where you might not know the exact number of elements in the array / collection. errors(2) will return Some("20") in your example and None if there isn't a value.
Replacing your innermost for loop with the above and hardcoding to exactly 3 parameters, you might then have something like
#defining( errors(0).getOrElse("") ) { messageId =>
#Message(messageId,errors(1).getOrElse(""),errors(2).getOrElse(""))
}
So I have a JSON file of a somewhat known format { String => JSON::Type, ... }. So it is basically of type Hash(String, JSON::Type). But when I try and read it from file to memory like so: JSON.parse(File.read(#cache_file)).as(Hash(String, JSON::Type)) I always get an exception: can't cast JSON::Any to Hash(String, JSON::Type)
I'm not sure how I am supposed to handle the data if I can't cast it.
What I basically want to do is the following:
save JSON::Type data under a String key
replace JSON::Type data with other JSON::Type data under a String key
And of course read from / write to file...
Here's the whole thing I've got so far:
class Cache
def initialize(#cache_file = "/tmp/cache_file.tmp")
end
def cache(cache_key : (String | Symbol))
mutable_cache_data = data
value = mutable_cache_data[cache_key.to_s] ||= yield.as(JSON::Type)
File.write #cache_file, mutable_cache_data
value
end
def clear
File.delete #cache_file
end
def data
unless File.exists? #cache_file
File.write #cache_file, {} of String => JSON::Type
end
JSON.parse(File.read(#cache_file)).as(Hash(String, JSON::Type))
end
end
puts Cache.new.cache(:something) { 10 } # => 10
puts Cache.new.cache(:something) { 'a' } # => 10
TL;DR I want to read a JSON file into a Hash(String => i_dont_care), replace a value under a given key name and serialize it to file again. How do I do that?
JSON.parse returns an JSON::Any, not a Hash so you can't cast it. You can however access the underlying raw value as JSON.parse(file).raw and cast this as hash.
Then your code is basically working (I've fixed a few error): https://carc.in/#/r/28c1
You can use use Hash(String, JSON::Type).from_json(File.read(#cache_file)). Hopefully you can restrict the type of JSON::Type down to something more sensible too. JSON::Any and JSON.parse_raw are very much a last resort compared to simply representing your schema using Hash, Array and custom types using JSON.mapping.
I have a JSON Array string like [1,2].I want to convert this in to Set.How can I do it in java 8 ?
This is my code
String languageStr = rs.getString("languages");
jobseeker.setLanguageIds(StringUtils.isEmpty(languageStr) ? null
: Arrays.stream(languageStr.split(","))
.map(Integer::parseInt)
.collect(Collectors.toSet()));
Getting error like this
java.lang.NumberFormatException: For input string: " 2"
The space in json array is the problem.Is there any solution?
This is my code after changes
String languageStr = rs.getString("languages");
String languages=languageStr.substring(1,languageStr.length()-1);
jobseeker.setLanguageIds(StringUtils.isEmpty(languages) ? null
: Arrays.stream(languages.split(","))
.map(String::trim)
.map(Integer::parseInt)
.collect(Collectors.toSet()));
Can I get the output in any other way withot using these 2 steps:
languages=languageStr.substring(1,languageStr.length()-1);
.map(String::trim)
You can use the trim method to remove leading and trailing withespace before parse it to Integer.
So your code will be this
Arrays.stream(languageStr.split(","))
.map(String::trim)
.map(Integer::parseInt)
Finally I got the solution
Changed code like this:
String languageStr = rs.getString("languages");
Set<Integer> languages = mapper.readValue(languageStr, typeFactory.constructCollectionType(Set.class, Integer.class));
jobseeker.setLanguageIds(StringUtils.isEmpty(languageStr) ? null : languages);
Using a TypeToken and the Google Gson library, you should be able to do that like below:
String languageJsonStr = rs.getString("languages");
Set<Integer> myLanguageSet = new Gson().fromJson(languageJsonStr, new TypeToken<HashSet<Integer>>(){}.getType());
I have this string:
var test = "toAdd";
I want to use it to extract data from JSON, like:
console.log(value.stats.test);
As you see, test is not correct, as it is just a string and can't be used, it is just not recognized at all. How do I make it recognize?
What you are attempting to do is this:
var someVar;
someVar.test = 'Sample';
someVar.test.attribute = 'Another sample';
// this:
console.log(someVar['test']['attribute']);
// will produce the same as this:
console.log(someVar['test'].attribute);
// as well as the same as this:
console.log(someVar.test['attribute']);
This will print "Another sample".
This has nothing to do with JSON. This is just javascript, and like anything where the . notation won't work switch to array notation:
foo.bar.baz = 'qux';
alert(foo['bar'].baz); // popup with 'qux'
^-----^-- note these
In your case, value.stats[test]. Now "test" isn't an array key, it's a variable whose value gets used as the key.
I'm trying to construct json text as show below. But the variables such as $token, $state, $failedServers are not been replaced with its value. Note- I don't want to use any module specifically for this to work, I just want some plain string to work. Can anyone help me ?
my $json = '{"serverToken":"$token", "state":"$state","parameters" :"$failedServers"}';
current output was:
{"serverToken":"$token", "state":"$state","parameters" :"$failedServers"}
needed output format:
{"serverToken":"1213", "state":"failed","parameters" :"oracleapps.veeralab.com,suntrust.com"}
Your variables are not being replaced, because they are inside of a single-quoted string--that is, they are inside a string quoted by ' characters. This prevents variable substitution.
You will also be much better off creating JSON using a JSON library, such as this one. Simply using a quoted string is very dangerous. Suppose your one of your variables ends up containing a special character; you will end up with invalid JSON.
{"serverToken":"123"ABC", "state":"offline", "paramameters":"bugs"}
If your variables come from user input, really bad things could happen. Imagine that $token is set to equal foo", "state":"online", "foo":"bar. Your resulting JSON structure would be:
{"serverToken":"foo", "state":"online", "foo":"bar", "state":"offline" ...
Certainly not what you want.
Possible solutions:
The most blatantly obvious solution is simply not to the ' quote character. This has the drawback of requiring you to escape your double quote (") characters, though, but it's easy:
my $json = "{\"serverToken\":\"$token\", \"state\":\"$state\",\"parameters\" :\"$failedServers\"}";
Another option is to use sprintf:
my $json = sprintf('{"serverToken":"%s", "state":"%s", "parameters":"%s"}', $token, $state, $failedServers);
But by far, the best solution, because it won't break with wonky input, is to use a library:
use JSON;
my $json = encode_json( {
serverToken => $token,
state => $state,
paramaters => $failedServers
} );