簡體   English   中英

使用JSON.Net從JSON序列化為XML的問題-C#

[英]Issues Using JSON.Net to serialize from JSON to XML - C#

我正在嘗試使用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.路徑“ Calculations.1.Calculation_Logic”,第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(Full_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"]}}} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM