当我们在string.Format中传入Json字符串时,会报”输入字符串的格式不正确“,这是因为json的"{"符号的问题,最开始我是想着用转义一下"{",但是转义后我发现原来的非json占位格式缺报错了,因为我破坏了它的占位格式

问题还是没有解决,但是最终从另一个角度解决了我的需求,那就是从把string.Format封装起来,在内部进行判断,如果它的orgs的length为0,那么我们就直接输出format,否则才调用string.Format,一般情况下我们不会在json字符串中使用占位符,所以,要么我们直接传入json字符串,要么把json作为orgs的一个传进去,这样基本就避免了报错了,看代码

  

class Program {
static void Main(string[] args) {
string str1 = "{\"aaa\":\"123\",\"bbb\":{\"ccc\":\"456\"}}";
Format(str1);
Format("ct:{0},rs:{1}", 123, 456);
Format("json:{0}", str1);
Console.ReadKey();
}
private static void Format(string msg, params object[] ps) {
if (ps.Length > 0) {
msg = string.Format(msg, ps);
}
Console.WriteLine(msg);
}
}

  

C# string.Format json格式字符串报错”输入字符串的格式不正确“的更多相关文章

  1. json格式的字符串使用string.Format()方法报错:输入字符串的格式不正确

    解决:把大括号转义一下就可以了啊,大括号的转义是两个{{  结尾是}}     今天看同事写的代码,发现他在使用string.format拼接类似json格式的数据时,大括号多了一对,感觉不对就查了查 ...

  2. ESL python调用C模块时传递unicode字符串报错问题解决

    在是用freeswitch时利用ESL的python调用时传递字符串报错 TypeError: in method 'ESLconnection_api', argument 2 of type 'c ...

  3. string.Format出现异常:输入字符串的格式不正确 Exception during StringFormat

    错误信息:Exception during StringFormat:输入字符串的格式不正确 “System.FormatException”类型的未经处理的异常在 mscorlib.dll 中发生 ...

  4. DataGridView 中发生以下异常: System.Exception: 是 不是 Decimal 的有效值。 ---> System.FormatException: 输入字符串的格式不正确。

    其实之前我自己是没测出这个问题的,但是一放到测试的手上就出来了,原因我知道在哪里改输什么东西,但是人家不知道啊.报错如下: --------------------------- “DataGridV ...

  5. Convert 输入字符串的格式不正确

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  6. c# string.format json字符串 formatException错误

    正常字符串的string.format是没问题的但是在拼接json的字符串的时候因为里面包含了 {}  花括号 里面又嵌套了 {0} {1} {2}这些要替换的关键字 所以会报错. 经过百度. 字符串 ...

  7. iOS中空字符串报错

    *参考: http://www.ithao123.cn/content-8030945.html *参考: http://www.cnblogs.com/ziyi--caolu/p/4825633.h ...

  8. string Format转义大括号:输入字符串的格式不正确。

    String.Format("{0} world!","hello") //将输出 hello world!,没有问题,但是只要在第一个参数的任意位置加上一个大 ...

  9. iOS解析JSON字符串报错Error Domain=NSCocoaErrorDomain Code=3840 "Invalid escape sequence around character 586."

    将服务器返回的JSON string转化成字典时报错: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid escape sequence ...

随机推荐

  1. Ansible用法playbook

    playbook文件 hello.yml --- - name: test_tasks [各个任务的总描述] hosts: webserver remote_user: root gather_fac ...

  2. spring cloud学习笔记三 Feign与Ribbon负载均衡的区别

    一.Feign的介绍 Feign一般比较书面的解释是:Feign是一个声明式的WebService客户端,使用Feign编写的WebService客户端更加简单,他的使用方法是定义一个接口,然后在上线 ...

  3. 固定内网ip的方法

    ip最后一位找一个不常用的,比如200之后的,ping不通它就用它. 子网掩码,默认网关保持和原来的一样. DNS要填公司的,网上查的不能用,因为他们是互联网上的.主备:XXXXXX/XX (之前填的 ...

  4. $Dsu$ $on$ $Tree$ 复习

    \(Dsu\) \(on\) \(Tree\) 复习 发现最近有点头晕,突然这东西就不会了,头疼了很久,决定写一份记录啊. 大致认识 适用范围一般在处理树上子树统计问题,不支持在线回答询问以及修改. ...

  5. bzoj4036 [HAOI2015]按位或 状压DP + MinMax 容斥

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4036 题解 变成 \(2^n-1\) 的意思显然就是每一个数位都出现了. 那么通过 MinMa ...

  6. Java字符串流学习

    字符串流 定义:字符串流,以一个字符为数据源,来构造一个字符流. 作用:在Web开发中,我们经常要从服务器上获取数据,数据返回的格式通过一个字符串(XML.JSON),我们需要把这个字符串构造为一个字 ...

  7. 终端、mac等小技巧——2019年10月18日

    1.新建finder窗口 cmd+N 2.查看文件夹结构 brew install tree tree命令行参数(只实用与安装了tree命令行工具): -a 显示所有文件和目录. -A 使用ASNI绘 ...

  8. Python中的"Special Method"

    The first thing to know about special methods is that they are meant to be called by the Python inte ...

  9. boost asio scalability and multithreading

    A library such as Boost.Asio is typically used to achieve greater efficiency. With no need to wait f ...

  10. 【Shiro】SpringBoot集成Shiro

    项目版本: springboot2.x shiro:1.3.2 Maven配置: <dependency> <groupId>org.apache.shiro</grou ...