user516883
user516883

Reputation: 9378

Reading JavaScriptSerializer c# back in javascript

In one of my webservice methods I serialized my object calling JavaScriptSerializer.serilize in c#. Now when that returns a string to my javascript I would like to be able to call the properties from the object. I tried, results.d.ID but that did not work. 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. 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\":\"[email protected]\",\"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\"}"}

Upvotes: 1

Views: 4995

Answers (4)

Dave Ward
Dave Ward

Reputation: 60580

It looks like your underlying problem is the manually JSON serialization.

Since ASP.NET will automatically JSON serialize the result of your method, your manually serialized jsonObject string is then getting serialized a second time. 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.

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:

{"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.

Upvotes: 0

amit_g
amit_g

Reputation: 31250

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

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

// embeddedJsonObj.ID is the ID you need.

Demo

Upvotes: 1

frenchie
frenchie

Reputation: 51937

Here's what it's all about: asp.net's ajax d

Upvotes: 1

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

As Inerdial said you have strange nested JSON value. If data is in format you actually want - use JSON.parse to re-parse values like:

JSON.parse(results.d).ID.

Upvotes: 3

Related Questions