我需要验证JSON有效内容以包含特定字段的JSON对象。据我所知,在Laravel的#include <iostream>
using namespace std;
struct B{
const char mem_var[];
};
struct A {
const char * value_in_struct ; // this line gives me a error message.
A(char *s):value_in_struct(s){}
A() { value_in_struct = NULL;}
};
void t(void) {
const char value[] = "att"; // this line was ok at compiling
std::cout << "value = " << value << std::endl;
//value[2] = 's'; // gives error
}
int main(){
A a(const_cast< char *>("abc"));
A b ;
b.value_in_struct = "bbc";
cout <<"a: "<<a.value_in_struct << endl;
cout <<"b: "<<b.value_in_struct << endl;
t();
//B bb; gives error for not initizaling mem_var
return 0;
}
请参阅下面的示例。
Controller.php这样
Illuminate\Http\Request
private static function getType($o) {
if (is_object($o)) {
return "Object";
} else if (is_array($o)) {
return "Array";
}
return "Unknown";
}
public function test(Request $request) {
$input = $request->all();
$response = [];
foreach ($input as $key => $value) {
$response[$key] = Controller::getType($value);
}
return response()->json($response);
}
是获取HTTP请求命中的函数。
以下是来自 Controller.php
的示例请求和响应请求
test
响应
{
"obj1": {},
"obj2": {
"hello": "world"
},
"arr1": [],
"arr2": ["hello world"]
}
我是否可以在此处验证字段{
"obj1": "Array",
"obj2": "Array",
"arr1": "Array",
"arr2": "Array"
}
和obj1
仅包含 JSON对象?
答案 0 :(得分:1)
Laravel的Illuminate\Http\Request
使用
json
函数中的JSON请求
json_decode($this->getContent(), true)
将true
用于第二个参数$assoc
会使json_decode
将所有对象转换为关联数组。
我对 Controller.php
进行了以下更改$input = $request->all();
已更改为
$input = json_decode($request->getContent());
以下是已修改 Controller.php
的示例请求和响应请求强>
{
"obj1": {},
"obj2": {
"hello": "world"
},
"arr1": [],
"arr2": ["hello world"]
}
<强>响应强>
{
"obj1": "Object",
"obj2": "Object",
"arr1": "Array",
"arr2": "Array"
}