0
var var1 = JSON.parse('[{"ItemId":1, "ItemName":"item 1\"", "Unit":"Nos","Remarks":null, "ConsumedQuantity":1.00},
        {"ItemId":1253, "ItemName":"item 2", "Unit":"Nos", "Remarks":null, "ConsumedQuantity":1.00}]');

var1.forEach(function (e) {
    Object.keys(e).forEach(function (key) {
        if (e[key] == id) {
           //doing some stuff here
        }
    });
});

This code works perfectly when the value in the JSON doesn't contain double quotes. This JSON is produced from a list of Model in MVC. I use @Html.Raw(Json.Encode(ViewBag.materialDetails)) to convert the list to JSON. When there is a double quote, then it doesn't enter into the forEach.

Any help is appreciated :)

1
  • 2
    should be two \\ to escape it when it is in a string "ItemName":"item 1\\"" Commented May 26, 2017 at 12:30

1 Answer 1

2

@Html.Raw(Json.Encode(ViewBag.materialDetails)) should return valid JSON data so you don't need to add ticks around it or JSON.parse it.

If you change

var var1 = JSON.parse('@Html.Raw(Json.Encode(ViewBag.materialDetails))');

to:

var var1 = @Html.Raw(Json.Encode(ViewBag.materialDetails));

it might work

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

2 Comments

@Sphinxxx Nice! :)
Perfect ! Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.