如何使用C#从XML字符串中获取JSON字符串?(How get JSON string from XML string using C#?)
我试图将XML字符串转换为C#对象,我在xml标记中有json string acle,如下所示,
<message> <data:gcm xmlns:data=\"google:mobile:data\">{\"message_type\":\"ack\",\"from\":\"sdhad4asd4a-sdasd45ds\",\"message_id\":\"-something\"}</data:gcm> </message>
我想从数据标签中获取json字符串我只想从xml上面找到这个字符串,
{\ “message_type \”:\ “ACK \”,\ “从\”:\ “sdhad4asd4a-sdasd45ds \”,\ “MESSAGE_ID \”:\ “ - 这是\”}
那么我怎样才能使用c#来实现这个目的。
先感谢您。
I am trying to convert XML string to C# object, I have json string acle in xml tag, as shown below,
<message> <data:gcm xmlns:data=\"google:mobile:data\">{\"message_type\":\"ack\",\"from\":\"sdhad4asd4a-sdasd45ds\",\"message_id\":\"-something\"}</data:gcm> </message>
I want json string from data tag I just want this string from above xml,
{\"message_type\":\"ack\",\"from\":\"sdhad4asd4a-sdasd45ds\",\"message_id\":\"-something\"}
So how can I get this using c#.?
Thank you in advance.
原文:
最满意答案
你使用单引号而不是双引号 - 因此它将内部括号视为字面值。
你应该交换他们的双引号(并逃避其他人):
$stmt = "{\"status\":{$status},\"message\":{$message}}";
或者你应该摆脱它们并手动连接:
$stmt = '{"status":' . $status . ',"message":' .$message . '}';
这些都应该足够了
You've used single quotes instead of double - therefore it's taken the internal braces as literal.
You should either swap them for double quotes (And escape the others):
$stmt = "{\"status\":{$status},\"message\":{$message}}";
Or you should get rid of them and concatenate manually:
$stmt = '{"status":' . $status . ',"message":' .$message . '}';
Either of these should be sufficient
相关问答
更多-
PHP字符串连接(PHP string concatenation)[2023-11-02]
只需使用. 用于连接。 你错过了$personCount增量! while ($personCount < 10) { $result .= $personCount . ' people'; $personCount++; } echo $result; Just use . for concatenating. And you missed out the $personCount increment! while ($personCount < 10) { $result ... -
尝试: $file = preg_replace('/[^a-z_\-]/i', '', $_GET['F']); 当然,如果他们试图破解你的页面,我会运行测试并将它们发送到IC3。 Try: $file = preg_replace('/[^a-z_\-]/i', '', $_GET['F']); Of course, I would just run a test and send them to IC3, if they're trying to hack your page.
-
出于某种原因,$ supervisorName被一堆尾随的空格和换行符拉了出来。 我用修剪来清理它,它完美无缺。 谢谢你们,我一整天都在这里。 For some reason the $supervisorName was being pulled with a bunch of trailing whitespaces and linebreaks. I used trim to clean it up and it works perfect. Thanks both of you, I would'v ...
-
简而言之,一旦你设置了一个字符串,它的设置。 对于类似的东西,它不受约束。 人们倾向于使用格式化的字符串来做这样的事情: $formattedString = "Hello %s".PHP_EOL; $possibleValues = [ "World", "John Doe" ]; foreach ($possibleValues as $value) { printf($formattedString, $value); } 打印: 你好,世界 你好John Doe 见它运行于: ...
-
php变量字符串连接(php variable string concatenation)[2022-11-30]
尝试逃避 $mail_from=$full_name . "<" . $user_obj->getEmailAddress() . ">"; 演示 ---运行以下代码: http : //writecodeonline.com/php/ function getEmailAddress(){ return "user@example.com"; } $full_name = "My Name"; $mail_from = $full_name ."<". getEmailAdd ... -
这个. 运算符是连接运算符。 您的第一个示例仅适用于echo函数(技术上它是一种语言结构,但不允许分割头发)接受多个参数,并且会打印每个参数。 因此,第一个示例是使用多个参数调用echo,并且它们都被打印出来,而第二个示例中所有字符串都是concatenate的,并且正在打印一个大字符串。 The . operator is the concatenation operator. Your first example only works because the echo 'function' (techn ...
-
你使用单引号而不是双引号 - 因此它将内部括号视为字面值。 你应该交换他们的双引号(并逃避其他人): $stmt = "{\"status\":{$status},\"message\":{$message}}"; 或者你应该摆脱它们并手动连接: $stmt = '{"status":' . $status . ',"message":' .$message . '}'; 这些都应该足够了 You've used single quotes instead of double - therefore it' ...
-
您最有可能在字符串中使用由浏览器呈现的html标记。 $foo = "