如何将字符串转换为json

时间:2016-09-30 06:58:38

标签: c# json asp.net-web-api

我有下面的代码,但我的json结果无法解析。我不知道为什么它不解析。请帮助我,类属性与json转换为c#相同

 using (WebClient client = new WebClient())
                        {
                            client.Headers.Add("API-Version: 5.0");
                            client.Headers.Add("Content-Type","application/json");

                             ss= client.UploadString(baseAddress + "/api/Jobs/MakePayment", json);
                        }

                    try
                    {
                      //  rtn = rtn.Replace("\", " ").Trim();

                        ResponseJudo objCls = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponseJudo>(ss);

                    }

我的回答json是这样的:

{\"receiptId\":\"5443635\",\"yourPaymentReference\":\"Test1964\",\"type\":\"Payment\",\"createdAt\":\"2016-09-30T07:46:50.9991+01:00\",\"result\":\"Success\",\"message\":\"AuthCode: 830476\",\"judoId\":100109768,\"merchantName\":\"\Khan\",\"appearsOnStatementAs\":\"APL/khn\",\"originalAmount\":\"11.10\",\"netAmount\":\"11.10\",\"amount\":\"11.10\",\"currency\":\"GBP\",\"cardDetails\":{\"cardLastfour\":\"3436\",\"endDate\":\"1220\",\"cardToken\":\""\",\"cardType\":1},\"consumer\":{\"consumerToken\":\"uToaxZzXSJngo0iL\",\"yourConsumerReference\":\"TestPayment2111\"},\"risks\":{\"postCodeCheck\":\"UNKNOWN\"}}"

1 个答案:

答案 0 :(得分:1)

通过https://jsonformatter.curiousconcept.com/运行JSON响应会显示多个错误,这些错误是由于JSON格式错误造成的。

例如,如果我们将\"替换为"以将字符串转换为原始格式,则cardToken看起来像这样

"cardToken":""","cardType":1}

因此,实际问题在字符串变量ss中。需要修复它以返回正确的JSON ...同样在值"\Khan"中,反斜杠被解释为转义字符。