简体   繁体   English

读取包含所有空格和缩进的原始 JSON 字符串的一部分

[英]Read part of ORIGINAL JSON string with all spaces and indents

My client posts following json data in the body to my API, is there an easy way to get hold of the abc node's value (including the {} as well) as JSON string from HttpContext.Request.Body ?我的客户端将正文中的以下 json 数据发布到我的 API,是否有一种简单的方法可以从HttpContext.Request.Body作为 JSON 字符串的abc节点的值(也包括{} )? the most important thing is I need to keep all original spaces or indent if any .最重要的是我需要保留所有原始空格或缩进(如果有)

PS the abc is always the first node, while def is the last node. PS abc总是第一个节点,而def是最后一个节点。

Thanks谢谢

{
   "abc": {
     ....
   },
   "def":"..."
}

You can try JObject which is part of Newtonsoft.Json library 您可以尝试JObject ,它是Newtonsoft.Json库的一部分

using (StreamReader r = new StreamReader(filepath))
{
    string json = r.ReadToEnd();
    var jobject = JObject.Parse(json);
    var getabcvalue = jobject["abc"];
    Console.WriteLine(getabcvalue.ToString());
}

最终,我决定先从Stream读取字符串,然后使用Regex作为这种特殊情况的解决方案,我知道这不是很好,但是因为空格和缩进是关键要求之一,为了简单起见,我很乐意交易暂时关闭性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM