Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 读取函数中包含JSON的字符串_C#_Asp.net Mvc_Json.net - Fatal编程技术网

C# 读取函数中包含JSON的字符串

C# 读取函数中包含JSON的字符串,c#,asp.net-mvc,json.net,C#,Asp.net Mvc,Json.net,我调用URL获取数据(URL不是我的) 答复是: string data = w.DownloadString(url); // response "MyCallBack([{\"symbol\":\"USDCHF\"},{\"symbol\":\"GBPUSD\"}])" 由于函数(“MyCallBack”)中包含JSON,如何获取该值 当我尝试进行解析时失败(错误是:“解析值时遇到意外字符:

我调用URL获取数据(URL不是我的) 答复是:

string data = w.DownloadString(url);
// response "MyCallBack([{\"symbol\":\"USDCHF\"},{\"symbol\":\"GBPUSD\"}])"
由于函数(“MyCallBack”)中包含JSON,如何获取该值 当我尝试进行解析时失败(错误是:“解析值时遇到意外字符:A.Path“”,第0行,位置0”。)

编辑

URL看起来像xxx.xxx.com?callback=MyCallBack 当我从URL“xxx.xxx.com”中删除回调时 答案仍在括号内 JSON

当我试着做语法分析时

var json1 = new JavaScriptSerializer().DeserializeObject(data);//or
var json2 = new JavaScriptSerializer().Deserialize<dynamic>(data);
我移除了括号的第一个和最后一个([{value,value]}),我得到[{value},{value}]
这是可行的(但我不确定这是不是可行的)

问题是,页面应该返回一个有效的JSON,而它们不会。因此,您必须从响应中删除所有无效字符

JSON字符串每次都以
[{
{
开头,这样您就可以知道JSON字符串从哪个索引开始以及何时结束

我写了一个非常简单易懂的方法:

    private string FilterJsonResponse(string responseWithJson)
    {
        var indexFirstJsonArray = responseWithJson.IndexOf("[{", StringComparison.Ordinal);
        var indexFirstJsonElement = responseWithJson.IndexOf("{\"", StringComparison.Ordinal);

        string filteredJson;

        if (indexFirstJsonArray < indexFirstJsonElement && indexFirstJsonArray != -1)
        {
            filteredJson = responseWithJson.Remove(0, indexFirstJsonArray);

            var indexLastJsonArray = filteredJson.IndexOf("}]", StringComparison.Ordinal) + 2;
            filteredJson = filteredJson.Remove(indexLastJsonArray, filteredJson.Length - indexLastJsonArray);
        }
        else
        {
            filteredJson = responseWithJson.Remove(0, indexFirstJsonElement);

            var indexLastJsonElement = filteredJson.IndexOf("\"}", StringComparison.Ordinal) + 2;
            filteredJson = filteredJson.Remove(indexLastJsonElement, filteredJson.Length - indexLastJsonElement);
        }

        return filteredJson;
    }
私有字符串过滤器JsonResponse(字符串响应WithJSON)
{
var indexFirstJsonArray=responseWithJson.IndexOf(“[{”,StringComparison.Ordinal);
var indexFirstJsonElement=responseWithJson.IndexOf(“{\”,StringComparison.Ordinal);
字符串过滤器;
if(indexFirstJsonArray

请注意此方法,它仅在无效字符不包含
“[{”
“{\”时有效

也许你可以修改
url
,让服务器返回JSON而不是JSONP?这样你就不需要在反序列化之前删除函数了。你是不是碰巧将这个
MyCallBack
作为参数传递给url?他们只给我类似“”的url好的,现在去掉
?callback=MyCallBack
,并将请求作为url直接发送到
xxxx.xxx.com
。当我删除回调(xxxx.xxx.com)时,我得到“([{“symbol\”:“USDCHF\”,{“symbol\”:“GBPUSD\”)而不是“[{“symbol\”:“USDCHF\”,{“symbol\:“GBPUSD\”)”您是否在Visual Studio调试器中查看此内容?这是字符串表示形式。您是否可以尝试反序列化通过JSON序列化程序接收的
数据
变量?如果您将此url放入浏览器地址栏,会发生什么情况?通常您应该得到正确的JSON响应。否则,我会非常惊讶w此服务器运行正常。谢谢,我使用您的代码(我删除了我编写的json.Substring(1,json.Length-2))并使用您的代码,这些代码非常有效且更加灵活。谢谢:)
    private string FilterJsonResponse(string responseWithJson)
    {
        var indexFirstJsonArray = responseWithJson.IndexOf("[{", StringComparison.Ordinal);
        var indexFirstJsonElement = responseWithJson.IndexOf("{\"", StringComparison.Ordinal);

        string filteredJson;

        if (indexFirstJsonArray < indexFirstJsonElement && indexFirstJsonArray != -1)
        {
            filteredJson = responseWithJson.Remove(0, indexFirstJsonArray);

            var indexLastJsonArray = filteredJson.IndexOf("}]", StringComparison.Ordinal) + 2;
            filteredJson = filteredJson.Remove(indexLastJsonArray, filteredJson.Length - indexLastJsonArray);
        }
        else
        {
            filteredJson = responseWithJson.Remove(0, indexFirstJsonElement);

            var indexLastJsonElement = filteredJson.IndexOf("\"}", StringComparison.Ordinal) + 2;
            filteredJson = filteredJson.Remove(indexLastJsonElement, filteredJson.Length - indexLastJsonElement);
        }

        return filteredJson;
    }