1

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\"}"}
1
  • 3
    You're wrapping a JSON object in a string that's a property of another JSON object. This is unusual, I'm guessing your use of JavaScriptSerializer is wrong somehow. Commented Jan 27, 2012 at 22:44

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This will work, but it's best to fix the underlying issue: encosia.com/…. Otherwise, he's going to be unnecessarily serializing and deserializing the data twice for every request.
1

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

2 Comments

Are you sure? Value of d is a string not a json-object.
@L.B: yes, it's a string and you parse it with var MyObject = JSON.parse(d);
1
var jsonObj = theObject; // Assuming it is parsed somehow

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

// embeddedJsonObj.ID is the ID you need.

Demo

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.