JSON.net返回格式错误的JSON

时间:2009-07-03 07:23:59

标签: web-services json json.net

我正在使用json.net来解析对象并将它们传递给我所做的Web服务。对象是LINQ对象。当我使用JQuery访问此Web服务时,它无法正确解析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":[]
     }
    ]
 }]

任何人都知道为什么这不起作用?

3 个答案:

答案 0 :(得分:2)

这里的实际问题是这个JSON嵌入在XML标签中。我通过使用此解析器解析JSON字符串来解决问题:http://www.json.org/js.html

对于cource来说,这并不理想,因为JQuery应该为我做这个,但是直到我找到一种方法让.NET-webservices返回原始的JSON字符串,而不是嵌入在XML标记中,这个解决方案起作用=)

答案 1 :(得分:0)

我想这是一个编码问题,如果你从记录中删除它会怎么样?

"resourcename":"Rødblyant 1"

答案 2 :(得分:0)

确保双方都使用UTF-8。我的猜测是你使用默认编码(如windows1252)编码字符串,或者接收端正在做类似的事情。

请记住,JSON必须是UTF-8,UTF-16(LE或BE)或UTF-32(LE或BE)。

要对此进行测试,请删除所有åø字符,看看它是否有效。如果是这样,它肯定是在一边或另一边编码。