Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.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.Net将JSON序列化为XML-C的问题#_C#_Json_Ajax_Xml_Json.net - Fatal编程技术网

C# 使用JSON.Net将JSON序列化为XML-C的问题#

C# 使用JSON.Net将JSON序列化为XML-C的问题#,c#,json,ajax,xml,json.net,C#,Json,Ajax,Xml,Json.net,我试图使用AJAX将JSON对象从客户端发送到服务器。我希望将JSON序列化为xml,并将其存储在SQL Server中 我正在执行POST并使用以下命令将JSON作为字符串发送: var Full_JSON = products_json[product_id]; var Message = ""; if (FULL_JSON == null) { Message = "No Products_JSON!"; } if (Messag

我试图使用AJAX将JSON对象从客户端发送到服务器。我希望将JSON序列化为xml,并将其存储在SQL Server中

我正在执行POST并使用以下命令将JSON作为字符串发送:

    var Full_JSON = products_json[product_id];
    var Message = "";

    if (FULL_JSON == null) {
        Message = "No Products_JSON!";
    }

    if (Message.length == 0) {
        $.ajax({
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",  
            url: "/Query.asmx/InsertXMLJSON",  
            data: "{'JSON':'" + JSON.stringify(Full_JSON) + "'}",
            success: function (Record) {
                if (Record.d == true) {
                    console.log("AJAX Success: " + JSON.stringify(Record));
                }
                else {
                    console.log("AJAX Fail: " + JSON.stringify(Record));
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log("Status: " + textStatus);
                console.log("Error: " + errorThrown);
            }
        })
    }
然后,我获取传递的“JSON”字符串并调用:

public static bool InsertXMLJSON(string UserEmail, string Time, string Product, string JSON)
{
    System.Xml.XmlDocument xmlJSON = JsonConvert.DeserializeXmlNode(JSON, "root");
}
调用此函数时,我会得到如下所示的异常:

解析值后遇到意外字符:Q.Path “计算.1.计算逻辑”,第1行,位置4167

以下是遇到该字符的JSON字符串片段:

\"Calculation_Logic\":\"Math.round((products_json[product_id][\"Questions\"][1][\"Answer\"])/(products_json[product_id][\"Questions\"][3][\"Answer\"]))\"
我注意到,方括号内的引文转义似乎是问题的一部分。如果我删除了引语,它会很好地解析它


知道这会导致问题吗?

在我看来,您对名为Full_JSON的简单字符串变量进行了字符串化。您试图发送它而不是JSON.stringify(完整的JSON)?如果没有完整的代码示例,很难理解您具体在串接什么,但这可能是原因所在。例如:

var str = '{"2003":{"1":{"2":["test"],"3":["test2"]}}}';
var obj = {"2003":{"1":{"2":["test"],"3":["test2"]}}};

//this one looks very similar to your problem
console.log( JSON.stringify(str) );  // {\"2003\":{\"1\":{\"2\":[\"test\"],\"3\":[\"test2\"]}}} 

//and this one is correct use of this component
console.log( JSON.stringify(obj) );  // {"2003":{"1":{"2":["test"],"3":["test2"]}}} 

在我看来,您正在对名为Full_JSON的简单字符串变量进行字符串化。您试图发送它而不是JSON.stringify(完整的JSON)?如果没有完整的代码示例,很难理解您具体在串接什么,但这可能是原因所在。例如:

var str = '{"2003":{"1":{"2":["test"],"3":["test2"]}}}';
var obj = {"2003":{"1":{"2":["test"],"3":["test2"]}}};

//this one looks very similar to your problem
console.log( JSON.stringify(str) );  // {\"2003\":{\"1\":{\"2\":[\"test\"],\"3\":[\"test2\"]}}} 

//and this one is correct use of this component
console.log( JSON.stringify(obj) );  // {"2003":{"1":{"2":["test"],"3":["test2"]}}} 

谢谢你的推荐。我上传了一段AJAX的代码片段。我直接在JSON对象上调用JSON.stringify。感谢您的推荐。我上传了一段AJAX的代码片段。我直接在JSON对象上调用JSON.stringify。