【问题标题】:Replace json brackets using c sharp?how to使用 c sharp 替换 json 括号?如何
【发布时间】:2015-06-29 09:49:24
【问题描述】:

这是我的json字符串

  {[ 
  {
    "CurrencyName": "nomi"  
    }, 
   { 
   "CurrencySign": "%%" 
     },
   {
    "PositiveFormat": "2"
   },
    {
       "NegativeFormat": "3"
    },
   {
    "CurrencyStatus": "45"
   }
  ]}

我想替换开始和结束括号 2 这些是

 {[ 
 ]}

只有然后我想将 json 存储在变量中,我的 c 清晰代码在这里

dynamic objEnteryVal = objEntry.GetValue("models")[0]["models"];

只有我必须使用 .replace 替换第一个和最后两个括号

【问题讨论】:

  • 你想用什么替换它们?另外,当您要求 c# 回复时,为什么还要包含 vb.net。
  • 你想达到什么目的?如果你想使用数据,只需反序列化它

标签: c# regex json


【解决方案1】:

嘿,我不认为替换会有很大帮助,我的意思是你必须自定义替换函数,因为 C# 中只有替换而不是替换AT 函数。你可以做的是使用下面的子字符串函数。

        string JSON = "{[SAME]}";
        int startPos=JSON.IndexOf("[") + 1;
        int LastPos=JSON.LastIndexOf("]");
        int length = JSON.Length - startPos - (JSON.Length - LastPos);
        JSON = JSON.Substring(startPos, length);

这将为您提供 {[SAME]} 的结果“SAME”。

【讨论】:

    猜你喜欢
    • 2012-08-25
    • 2015-07-30
    • 2021-07-13
    • 2019-06-03
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 2011-05-04
    相关资源
    最近更新 更多