2009-07-03 150 views
1

我使用json.net解析的對象,並將其提交給我做了一個web服務。這些對象是LINQ對象。當我使用JQuery訪問此webservice時,它不能正確解析JSON。似乎有一些錯誤的格式..JSON.net回畸形的JSON

的JSON字符串我得到的是這樣一個:

[{"typeid":1, 
    "typename":"binders", 
    "description":"test", 
    "RESOURCEs": 
    [{"resourceid":4, 
     "resourcename":"Binders 1", 
     "description":"Pakke med hele fire binders!!!", 
     "typeid":1, 
     "RESERVATIONLINEs":[] 
    },  
    {"resourceid":10, 
     "resourcename":"xxx", 
     "description":"xxx", 
     "typeid":1, 
     "RESERVATIONLINEs":[] 
    } 
    ] 
}, 
{"typeid":2, 
    "typename":"blyant", 
    "description":"også dyrt", 
    "RESOURCEs": 
    [{"resourceid":5, 
     "resourcename":"Gråblyant 1", 
     "description":"Fin og grå", 
     "typeid":2, 
     "RESERVATIONLINEs":[] 
    }, 
    {"resourceid":6, 
     "resourcename":"Rødblyant 1", 
     "description":"Må spisses ofte", 
     "typeid":2, 
     "RESERVATIONLINEs":[] 
    } 
    ] 
}, 
{"typeid":4, 
    "typename":"Penn", 
    "description":"tester", 
    "RESOURCEs": 
    [{"resourceid":7, 
     "resourcename":"Penn 1", 
     "description":"Blå og fin", 
     "typeid":4, 
     "RESERVATIONLINEs":[] 
    }, 
    {"resourceid":11, 
     "resourcename":"xxx", 
     "description":"xxx", 
     "typeid":4, 
     "RESERVATIONLINEs":[] 
    } 
    ] 
}, 
{"typeid":5, 
    "typename":"Kajakk", 
    "description":"Dette er en type båt", 
    "RESOURCEs": 
    [{"resourceid":1, 
     "resourcename":"Havkajakk 1", 
     "description":"FOr havbruk", 
     "typeid":5, 
     "RESERVATIONLINEs":[] 
    }, 
    {"resourceid":2, 
     "resourcename":"Havkajakk 2", 
     "description":"For havbruk", 
     "typeid":5, 
     "RESERVATIONLINEs":[] 
    } 
    ] 
}, 
{"typeid":6, 
    "typename":"3G-modem", 
    "description":"Fra NetCom", 
    "RESOURCEs": 
    [{"resourceid":3, 
     "resourcename":"3G-modem 1", 
     "description":"Gammelt og ustabilg", 
     "typeid":6, 
     "RESERVATIONLINEs":[] 
    }, 
    {"resourceid":12, 
     "resourcename":"xxx", 
     "description":"xxx", 
     "typeid":6, 
     "RESERVATIONLINEs":[] 
    } 
    ] 
}, 
{"typeid":7, 
    "typename":"Minnepinne", 
    "description":"på 1 KB", 
    "RESOURCEs": 
    [{"resourceid":8, 
     "resourcename":"Minnepinne 1", 
     "description":"1 KB (!)", 
     "typeid":7, 
     "RESERVATIONLINEs":[] 
    }, 
    {"resourceid":9, 
     "resourcename":"Minnepinne 2", 
     "description":"20 PB", 
     "typeid":7, 
     "RESERVATIONLINEs":[] 
    } 
    ] 
}] 

任何人都有一個線索,這是爲什麼不工作?

回答

2

這裏的實際問題是這個JSON被嵌入到XML標籤中。我通過使用本解析器解析JSON字符串解決了這個問題:http://www.json.org/js.html

五言這是不理想的,在jQuery應該這樣做對我來說,但直到我找到一種方法對.NET的Web服務返回原JSON字符串,不嵌入XML標籤,此解決方案工作=)

0

我想這是一個編碼的問題,如果你從你的記錄中刪除這是什麼?

"resourcename":"Rødblyant 1" 
0

請確保您在兩側都使用UTF-8。我的猜測是,要麼使用默認編碼(如windows1252)編碼字符串,要麼接收端做類似的事情。

記住,JSON必須爲UTF-8,UTF-16(LE或BE),或UTF-32(LE或BE)。

要測試這個,擺脫所有åø字符,看看它是否有效。如果是這樣,它肯定是編碼在一邊或另一邊。