- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
事情是这样的:我已经在大型系统 (PHP) 上工作了几年,现在,我决定放弃部分繁重的工作,转而使用 golang 脚本。
到目前为止,我将一些 php 脚本复制到了一个 go 版本中。然后,我能够对哪个选项更好进行基准测试(好的,我知道 go 更快,但我需要 curl 或 sockets 进行通信,所以,我必须检查它是否仍然值得)。
其中一个脚本只是生成一个随机代码,检查这个新代码是否已经被使用(在mysql db上),如果没有,记录新代码并返回它,如果已经被使用,就递归调用函数再次直到找到独占代码。非常简单。
我已经在 php 中有了这个代码生成器,所以,在 go 中写了一个新的,被称为带有 json 参数的 http/post。使用 linux 终端,我称之为
curl -H [headers] -d [jsondata] [host]
然后我得到一个非常简单的 json
{"locator" : "XXXXXX"}
之后,我编写了一个简单的 php 脚本来调用脚本并检查每个脚本完成需要多长时间,例如:
<?php
public function indexAction()
{
$phpTime = $endPHP = $startPHP =
$goTime = $endGO = $startGO = 0;
// my standard function already in use
ob_start();
$startPHP = microtime(true);
$MyCodeLocator = $this->container->get('MyCodeLocator')->Generate(22, 5);
$endPHP = microtime(true);
ob_end_clean();
sleep(1);
// Lets call using curl
$data = array("comon" => "22", "lenght" => "5");
$data_string = json_encode($data);
ob_start();
$startGO = microtime(true);
$ch = curl_init('http://localhost:8888/dev/fibootkt/MyCodeGenerator');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
curl_close($ch);
$endGO = microtime(true);
ob_end_clean();
$result_rr = json_decode($result);
// tst just echo the variable in a nice way, second parameter means no kill execution
tst($result, 1); // HERE IS MY PROBLEM, please read below
tst($result_rr, 1); // IT SHOW NULL
sleep(1);
// just to have params for comparision, always try by shell script
ob_start();
$startShell = microtime(true);;
$exec = "curl -H \"Content-Type: application/json\" -d '{\"comon\":\"22\"}' http://localhost:8888/dev/fibootkt/MyCodeGenerator";
$output = shell_exec($exec);
$endShell = microtime(true);
ob_end_clean();
tst(json_decode($output),1); // here show a stdclass with correct value
tst($output,1); // here shows a json typed string ready to be converted
// and here it is just for show the execution time ...
$phpTime = $endPHP - $startPHP;
$goTime = $endGO - $startGO ;
$shellTime = $endShell - $startShell;
tst("php " . $phpTime, 1);
tst("curl ". $goTime, 1);
tst("shell " . $shellTime, 1);
然后我从 GO 得到结果:通过 Shell 脚本:
{"locator" : "DPEWX22"}
因此,这个非常容易解码为 stdobj。
但是,使用 curl,操作更快!所以,我想使用它。但是,curl 请求响应如下:
{"Value":"string","Type":{},"Offset":26,"Struct":"CodeLocatorParams","Field":"lenght"}
{"locator":"DPEWX22"}
当我尝试对其进行解码时,结果为空!!!
CodeLocatorParams 是我在 go 中用来获取 post 参数的结构类型,如下所示
所以,这是我的问题:为什么 GO 返回这个?如何避免。
我有另一个类似的 go 脚本,它不带参数并响应类似的 json(但在这种情况下,是一个二维码图像路径)并且它工作正常!
我的 go 函数:
type CodeLocatorParams struct {
Comon string `json:"comon"`
Lenght int `json:"lenght"`
}
func Generate(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
fmt.Println(err)
panic(err)
}
if err := r.Body.Close(); err != nil {
panic(err)
}
// retrieve post data and set it to params which is CodeLocatorParams type
var params CodeLocatorParams
if err := json.Unmarshal(data, ¶ms ); err != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(422) // unprocessable entity
if err := json.NewEncoder(w).Encode(err); err != nil {
fmt.Println(err)
panic(err)
}
}
var result struct{
Locator string `json:"locator"`
}
// here actually comes the func that generates random code and return it as string, but for now, just set it static
result.Locator = "DPEWX22"
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
json.NewEncoder(w).Encode(result)
}
最佳答案
解析传入的 JSON 时出错。此错误以 {"Value":"string","Type":{},"Offset":26,"Struct":"CodeLocatorParams","Field":"lenght"} 形式写入响应
。处理程序继续执行并写入正常响应 {"locator":"DPEWX22"}
。
修复方法如下:
关于php - GoLang Web 服务器在 Json 响应中发送参数结构的描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48028207/
在一个环境中,我有 pandas 版本 0.17.0 和 numpy 版本 1.10.1。在另一个环境中,我有 pandas 版本 0.18.1 和 numpy 版本 1.10.4。 我运行这段代码
This question already has answers here: Default stringify for objects, equivalent to Java's toString
我一直在尝试为我的profile命令嵌入设置一个人们可以更改的简历。我认为它应该有效,但我的代码似乎有问题。 相关代码如下: const PREFIX = '!'; var bio = {}; cli
我正在尝试获取网站图标、网站标题和外部 URL 列表的描述,最好使用 jquery。我已经成功地为我的网址同步了谷歌的图标服务,任何人都可以阐明如何实现网站标题和描述吗?这是我到目前为止获得图标的内容
我在尝试运行代码时收到错误。找不到问题出在哪里。我可能遗漏了一些小细节,如果您能纠正它那就太好了。 计算Servlet import java.io.IOException; impo
我的数据库中有两个字段,一个是描述(TEXT),另一个是short_desc(VARCHAR-200)。 当我显示搜索结果时,我显然使用了short_desc,当有人点击该项目时,他们会得到完整的描述
当我尝试通过ajax和Jquery调用 Controller 上的save方法时,我正在使用Spring MVC、Jquery、Hibernate和tomcat。单击“保存”按钮时,我在 tomcat
我试图使用describe()来获取一些描述性统计数据,但获取了应该是数字的nan值。 我尝试使用 axis=0 或 axis=1,而 axis=1 产生了正确的数值,但这不是正确的数值我需要; ax
我有超过 1 亿个字符串要存储在文件系统中。与字符串 (~255Chars utf8) 一起,将有两个日期和一些定义其属性的整数值。 我可以将它们放在一个 CSV 文件中,但它会很大。我可以将几个较小
有没有办法将 JavaScript 合并到 Jenkins 顶部的描述字段中? 每当我添加脚本标签时,当您查看源代码时,它都会被 Jenkins 删除。 如果有人有建议或方向指出我,那就太好了。
您如何获得 SEH 的名称和/或描述?异常无需必须将字符串硬编码到您的应用程序中? 我尝试使用 FormatMessage(),但它有时会截断消息,即使您指定忽略插入也是如此: __asm { //
如果我在 MySQL 中使用 CREATE TRIGGER 语法创建一个触发器,我该如何附加注释来描述它?我的意思是在声明中或之后对我来说是一样的。 我想不通。对于表格,您可以在声明末尾添加 COMM
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
当开发多媒体应用或者游戏应用的时候,需要使用音量控制键来设置程序的音量大小。在Android系统中有多中音频流,通过Activity中的函数 setVolumeControlStream(int s
Slick DSL 允许通过两种方式在表中创建可选字段。 对于这个案例类: case class User(id: Option[Long] = None, fname: String, lname:
如果不属于默认命名空间,我如何描述 pod 信息。使用默认命名空间我没有任何问题。 但我想获得与命名空间对齐的特定 pod 的信息。 但是,当我想描述我可以制作的同一个 pod 时,请参阅 我尝试使用
在我使用过的几乎所有 vim 副本中,程序都会在替换文本后给出更改的描述。 (例如,将显示类似“20 行 92 个替换”之类的内容。) 我现在正在使用默认情况下不这样做的 vim 副本。 是否有一个简
我正在编写规范,需要描述一些 JSON 对象。单独的文本和选项卡往往会使大型 JSON 变得过于困惑。是否有任何在线(最好)工具可以创建类似于 http://www.json.org/ 上的图表的工具
我正在尝试通过 DNS 将我的 Kubernetes 部署连接在一起。 我有一个 Java (Spring Boot) 部署和一个 javascript (node.js) 部署,两者都通过默认的 C
我只是在学习 WebGL 图形编程。 我正在检查包含该语句的某人的代码 // multiply the position by the matrix. gl_Position = vec4((u_ma
我是一名优秀的程序员,十分优秀!