简体   繁体   English

用javascript读取JavaScriptSerializer c#

[英]Reading JavaScriptSerializer c# back in javascript

In one of my webservice methods I serialized my object calling JavaScriptSerializer.serilize in c#. 在我的一种Web服务方法中,我在c#中序列化了调用JavaScriptSerializer.serilize的对象。 Now when that returns a string to my javascript I would like to be able to call the properties from the object. 现在,当它向我的JavaScript返回一个字符串时,我希望能够从该对象调用属性。 I tried, results.d.ID but that did not work. 我尝试了result.d.ID,但是没有用。 Here is what it returns. 这是它返回的内容。 Thanks for any help. 谢谢你的帮助。

 JavaScriptSerializer oSerializer = new JavaScriptSerializer();
        Inventory item = Inventories.GetInventoryByID(inventoryID);
        string jsonObject = oSerializer.Serialize(item);

For example I would like to get the ID out. 例如,我想获取ID。 how would I do that? 我该怎么办?

{"d":"{\"ID\":\"589652cf-2ccd-49c1-b457-f2793a2a2424\",\"Brand\":{\"ID\":\"b728281b-cf3c-4ee0-ba3d-a3573b886b14\",\"Name\":\"Puma1\",\"ParentBrand\":null,\"BrandChildren\":{\"IsValueCreated\":false,\"Value\":[]}},\"DateAdded\":\"\\/Date(1327695794000)\\/\",\"AddedBy\":{\"ID\":\"d6e1f2e7-f8d1-4809-aadd-4cacd5c2bc43\",\"Email\":\"mojo@yahoo.com\",\"FirstName\":\"maurice\",\"MiddleInitial\":\"l\",\"LastName\":\"bachelor\",\"Address\":\"111 main st\",\"Phone\":\"2162330333\",\"IsAdmin\":true,\"DateJoined\":\"\\/Date(-62135578800000)\\/\",\"HasPurchased\":false,\"AgreeTerms\":true,\"LastPurchaseDate\":null,\"Password\":\"maurice\",\"CompanyName\":\"sneakers101\",\"AllowEmail\":false,\"PurchaseOrders\":{\"IsValueCreated\":false,\"Value\":[]}},\"LastUpdated\":\"\\/Date(1327688594000)\\/\",\"Instock\":true,\"NumberInStock\":12,\"MainPictureUrl\":\"\",\"AlternativePictureUrl\":\"\",\"ThumbNailUrl\":\"\",\"Price\":12.99,\"Like\":0,\"Discount\":1,\"ItemReleaseDate\":\"\\/Date(568011600000)\\/\",\"ItemCondition\":\"Great\",\"Size\":12,\"ItemNumber\":3,\"IsFavorite\":false,\"Details\":\"test Details\",\"Name\":\"Test\"}"}

As Inerdial said you have strange nested JSON value. 正如Inerdial所说,您有奇怪的嵌套JSON值。 If data is in format you actually want - use JSON.parse to re-parse values like: 如果数据确实是您想要的格式,请使用JSON.parse重新解析值,例如:

JSON.parse(results.d).ID . JSON.parse(results.d).ID

这就是全部内容: asp.net的ajax d

var jsonObj = theObject; // Assuming it is parsed somehow

var embeddedJsonObj = $.parseJSON( jsonObj.d );

// embeddedJsonObj.ID is the ID you need.

Demo 演示版

It looks like your underlying problem is the manually JSON serialization . 看来您的根本问题是手动JSON序列化

Since ASP.NET will automatically JSON serialize the result of your method, your manually serialized jsonObject string is then getting serialized a second time. 由于ASP.NET将自动对方法的结果进行JSON序列化,因此手动序列化的jsonObject字符串将再次进行序列化。 That's why Alexei's answer works. 这就是阿列克谢的答案行之有效的原因。 jQuery deserialized it once, leaving you with a .d property containing a JSON string, and then JSON.parse() deserialized that inner JSON string. jQuery将其反序列化一次,为您提供一个包含JSON字符串的.d属性,然后JSON.parse()反序列化该内部JSON字符串。

However, that is doubly inefficient because your data is being serialized twice and deserialized twice. 但是,这是双重低效的,因为您的数据被序列化了两次并反序列化了两次。 Instead, just do this: 相反,只需执行以下操作:

[WebMethod]
public Inventory GetInventoryById(inventoryID) {
  return item = Inventories.GetInventoryByID(inventoryID);
}

Then, ASP.NET will return JSON that looks like this: 然后,ASP.NET将返回如下所示的JSON:

{"d": {"ID": "589652cf-2ccd-49c1-b457-f2793a2a2424", /* the rest of your data here */ }}

Which jQuery (assuming you're using jQuery) will automatically JSON.parse() since ASP.NET responds with an application/json Content-Type, and then you'll be able to index into like results.d.ID . 由于ASP.NET会使用application/json Content-Type进行响应,因此哪个jQuery(假设您使用的是jQuery)将自动JSON.parse() ,然后您就可以将其索引为results.d.ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM