坚持将此 javascipt 代码(从 SO)“转换”为 php。
考虑 "AAA='111' BBB='222' DD='333' CC='dao@toto.fr'" 来自先前 json 对象键的值。
1 - 从一些 API 帖子中收到此 json
{
"first_key": "first_value",
"sec_key": "sec_value",
"third_key": "AAA='111' BBB='222' DD='333' CC='dao@toto.fr'",
}
2 - 期望像这样将 Third_key 值导出为新的 json
{ "AAA": "111", "BBB": "222", "DD": "333", "CC": "dao@toto.fr" }
所以,
<body>
<script type="text/javascript">
var input="AAA='111' BBB='222' DD='333' CC='dao@toto.fr'";
var result={};
input.split("'").forEach(function(value,i,arr){
if(i%2===0) return;
var key=arr[i-1].trim().replace("=","");
result[key]=value;
});
console.log(result);
</script>
</body>
在控制台中得到这个,大约是我想要的:
Object { AAA: "111", BBB: "222", DD: "333", CC: "dao@toto.fr" }
预期输出:
Object { "AAA": "111", "BBB": "222", "DD": "333", "CC": "dao@toto.fr" }
如何在 PHP 中获得预期的输出?搜索引擎将我发送到与我要查找的内容无关的 json_encode/json_decode 函数。
最佳答案
在 PHP 中,您可以非常简单地使用正则表达式来提取名称和值,然后使用 array_combine() 将其结果组合到关联数组中,然后使用 json_encode( ) 结果数组...
$third_key = "AAA='111' BBB='222' DD='333' CC='dao@toto.fr'";
preg_match_all("/(\w*)='(.*?)'/", $third_key, $matches);
print_r($matches);
echo json_encode(array_combine($matches[1], $matches[2]));
这给出了...
Array
(
[0] => Array
(
[0] => AAA='111'
[1] => BBB='222'
[2] => DD='333'
[3] => CC='dao@toto.fr'
)
[1] => Array
(
[0] => AAA
[1] => BBB
[2] => DD
[3] => CC
)
[2] => Array
(
[0] => 111
[1] => 222
[2] => 333
[3] => dao@toto.fr
)
)
{"AAA":"111","BBB":"222","DD":"333","CC":"dao@toto.fr"}
print_r($matches); 只是为了展示正则表达式如何将原始字符串拆分成它的部分,以及最后一行如何创建结束数组。
关于javascript - 从 json 键导出一些值作为新的 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55678919/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser