0

When trying to parse the serialized string from an object in json, I keep receiving an error saying it cant be parsed. Popwe class comprises of three decimal properies (peak, offpeak, weekend)

I have a object which comprises of the below classes

public class MinimumsAndParentRates {
    public MinimumsAndParentRates() {
        PerCall = new ListPopwes(); 
        PerMinute = new ListPopwes();
    }

    public ListPopwes PerCall { get; set; }
    public ListPopwes PerMinute { get; set; }
}

public class ListPopwes {
    public ListPopwes() {
        MinimumMargin = new Popwe();
        MinimumRetention = new Popwe();
        MasterRate = new Popwe();
    }

    public Popwe MinimumMargin { get; set; }
    public Popwe MinimumRetention { get; set; }
    public Popwe MasterRate { get; set; }
}

I then set a view model property like so

viewmodel.JsonData = JsonConvert.SerializeObject(obj);

When I write out the result I get the below string

{
    "PerCall":
    {
        "MinimumMargin":
        {
            "Peak":0.00000000,
            "OffPeak":0.00000000,
            "Weekend":0.00000000
        },
        "MinimumRetention":
        {
            "Peak":0.00000000,
            "OffPeak":0.00000000,
            "Weekend":0.00000000
        },
        "MasterRate":
        {
            "Peak":0.00000000,
            "OffPeak":0.00000000,
            "Weekend":0.00000000
        }
    },
    "PerMinute":
    {
        "MinimumMargin":
        {
            "Peak":0.00000000,
            "OffPeak":0.00000000,
            "Weekend":0.00000000
        },
        "MinimumRetention":
        {
            "Peak":0.20000000,
            "OffPeak":0.20000000,
            "Weekend":0.20000000
        },
        "MasterRate":
        {
            "Peak":1.00000000,
            "OffPeak":2.00000000,
            "Weekend":3.00000000
        }
    }
}

However when I try to parse this as JSON using jquery I receive the error, I have tried the below.

    console.log("@Model.JsonData");
    var array = "@Html.Raw(Json.Encode(Model.JsonData))" //Error occurs on this line when using Html.Raw;
    var obj = $.parseJSON(array); //Error occurs on this line when not using Html.Raw
    console.log(array);

The following are the error messages I get

@Html.Raw - SyntaxError: missing ; before statement

parsing - SyntaxError: JSON.parse: unexpected character
2
  • 2
    What "error" do you receive? What exactly does it say? Commented Jan 9, 2014 at 19:31
  • @RocketHazmat I have added the error messages into the question Commented Jan 9, 2014 at 19:48

2 Answers 2

4

You already serialized your data to JSON using JsonConvert.SerializeObject(obj); when you added it to the model. You don't need to serialize it a second time using Json.Encode.

Try: var array = "@Html.Raw(Model.JsonData)";

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

5 Comments

Also, you can remove 1 more step by removing the quotes, effectively removing the need to convert the json into an array/object. Though, since you're wrapping it in double quotes, won't this cause a syntax error since the json contains unescaped double quotes?
@KevinB Removing the Quotes actually worked. So instead of var array = "@Html.Raw(Json.Encode(Model.JsonData))" its actually var array = @Html.Raw(Json.Encode(Model.JsonData)), which throws up an error in intellisense on visual studio but compiles and runs ok.
well, you're mixing server-side and client-side code, i'd expect it to tell you it's wrong. :p @Html is invalid javascript, which is likely why the editor throws an error.
Should I mark this answer as the accepted one, as it was answered technically in the comment.
I would, it is mostly correct. that or write your own answer that shows the completely correct answer
1

The problem is two-fold. First, you're double encoding the json resulting in double quotes being replaced with ". Secondly, once you stop double encoding it, the double quotes inside the string will break due to the double quote outside. the most consistent solution to both would be to not wrap it in double quotes and not double encode it.

var array = @Html.Raw(Model.JsonData);

Though, this leaves you completely open to script injection, therefore i suggest going back to wrapping it in double quotes and then properly escaping the inner double quotes using \" on the server-side.

Of course, that script injection can only happen if the source of that json can be maliciously modified.

Comments

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.