我使用php变量动态创建了以下json,它给了我:
{"message":"invalid json"}
代码:
$json = '{
"sex": "'.$gender.'",
"age": '.$age.',
"evidence": [
{"id": "'.$symptom_id1.'", "choice_id": "present", "initial": true},
{"id": "'.$symptom_id2.'", "choice_id": "present", "initial": true},
{"id": "p_7", "choice_id": "'.$test1.'"},
{"id": "p_8", "choice_id": "'.$test2.'"},
{"id": "p_9", "choice_id": "'.$test3.'"},
{"id": "p_10", "choice_id": "'.$test4.'"},
{"id": "p_28", "choice_id": "'.$test5.'"},
{"id": "'.$location.'", "choice_id": "present"}
],
"extras": {
"disable_groups": true
}
}';
$ch = curl_init('https://api.infermedica.com/v2/diagnosis');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($json),
'app_id: '. APP_ID,
'app_key: '. APP_KEY,
'Interview-Id: '.$interview_id,
'User-Id: '. $hasheduser_id
]);
$result = curl_exec($ch);
echo '<pre>';
print_r( $result );
echo '</pre>';
对值进行硬编码以代替变量可以正常工作。作为一个新手,我无法理解为什么它没有良好的形成。
请求帮助。
答案 0 :(得分:1)
您永远不应该尝试自己创建有效的JSON,它只会导致安全问题和错误,而是使用数据结构并将其传递给 public Form1()
{
InitializeComponent();
getAvailablePorts();
}
void getAvailablePorts()
{
// Get a list of existing ports
String[] ports = SerialPort.GetPortNames();
String[] baudrates = new String[] { "9600", "115200" };
// Insert the lists into the combo boxes
comboBox1.Items.AddRange(ports);
comboBox2.Items.AddRange(baudrates);
// Select the default setting if at least a com port is found.
if (ports.Count() != 0)
{
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 1;
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (comboBox1.Text == "" || comboBox2.Text == "")
{
textBox1.Text = "Please select port settings";
}
else
{
serialPort1.PortName = comboBox1.Text;
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
serialPort1.DataReceived += new
SerialDataReceivedEventHandler(serialPort1_DataReceived);
serialPort1.Open();
progressBar1.Value = 100;
button1.Enabled = false;
button2.Enabled = true;
}
}
catch (UnauthorizedAccessException)
{
textBox1.Text = "UnauthorizedAccess";
}
}
private void serialPort1_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string value = sp.ReadExisting();
processValue(value);
}
//Delegate to update received data in the textbox
private delegate void processValueDelegate(string value);
private void processValue(string value)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new processValueDelegate(processValue),
new object[] { value });
}
else
{
// ... update GUI in here ...
textBox1.AppendText(value);
DataExtraction(value);
}
}
string UnreadBuffer = "";
private void DataExtraction(string RxString)
{
UnreadBuffer += RxString;
// Search for Data Header "<=>"
int start = UnreadBuffer.IndexOf("<=>");
int delimiter = UnreadBuffer.IndexOf("#");
if (start >= 0)
{
string row = UnreadBuffer.Substring(start);
if (delimiter >= 0)
{
string[] ExtractedDataFields = row.Split('#');
if (ExtractedDataFields.Length >= 7)
{
textBox2.Text = string.Format("{0:HH:mm:ss tt}", DateTime.Now);
textBox3.Text = ExtractedDataFields[1];
textBox4.Text = ExtractedDataFields[2];
textBox5.Text = ExtractedDataFields[3];
textBox6.Text = ExtractedDataFields[5];
UnreadBuffer = "";
}
}
}
}
。
您使用的示例数据结构:
json_encode
然后编码:
$to_json = [
'sex' => $gender,
'age' => $age,
'evidence' => [
['id' => $symptom_id1, 'choice_id' => 'present', 'initial' => true],
['id' => $symptom_id2, 'choice_id' => 'present', 'initial' => true],
['id' => 'p_7', 'choice_id' => $test_1],
['id' => 'p_8', 'choice_id' => $test_2],
['id' => 'p_9', 'choice_id' => $test_3],
['id' => 'p_10', 'choice_id' => $test_4],
['id' => 'p_28', 'choice_id' => $test_5],
['id' => $location, 'choice_id' => 'present'],
],
'extras' => [
'disabled_groups' => true
],
];
希望这有帮助。
答案 1 :(得分:0)
由于变量第二行有拼写错误,您的JSON无效。
"age": '.$age.',
应该是
"age": "'.$age.'",
或者什么会更好
"age": "$age",
但是,完成您尝试做的事情的最佳方法是创建一个PHP数组,并将其传递给json_encode。
$array = ['hello, i am a key'=>'i am some data'];
var_dump(json_encode($array));