0

I'm trying to add to the JsonResult object a parsed Json string, but I couldn't do it, the parser object in the browser is shown as:

"filter":[[[]],[[[],[]]]]

This is the full code

    public JsonResult AjaxStandardGet(int id)
    {
        Standard ec = db.Standard.FirstOrDefault(s => s.IdStandard == id);

        // inside filter: { "KeyDynamic1": "Value1", "KeyDynamic2": [ "AAA", "DDD"] }
        var filter = JsonConvert.DeserializeObject(ec.Filter);

        return Json(new
        {
            ec.IdStandard,
            ec.Description,
            filter,
            ec.Observations,
            Services = ec.StandardServices.Select(s => new {
                s.IdStandardServices,
                Tecnology = s.Tecnology?.Nombre,
                ServiceType = s.ServiceType?.Description,
                s.Quantity,
                s.Cost,
                Options = (!string.IsNullOrEmpty(s.Options) ? s.Options.Split(',') : null),
                Total = s.Quantity * s.Cost
            }),
            Success = true
        });
    }

I can't create the model object because the filters are not allways the same.

I tried this:

Dictionary<string, object> filter = JsonConvert.DeserializeObject<Dictionary<string, object>>(ec.Filter);

And I get

"filter":{"KeyDynamic1":"Value1","KeyDynamic2":[[],[]]}

1 Answer 1

1

I suggest you to JToken or dynamic:

JToken filter = JToken.Parse(ec.Filter);

dynamic filter = JsonConvert.DeserializeObject<dynamic>(ec.Filter);

Here is working fiddle.

Update

It seems that JavaScriptSerializer is not able to do it. So you can serialize your result using Newtonsoft.Json and return it as a string:

var result = new
    {
        ec.IdStandard,
        ec.Description,
        filter,
        ec.Observations,
        Services = ec.StandardServices.Select(s => new {
            s.IdStandardServices,
            Tecnology = s.Tecnology?.Nombre,
            ServiceType = s.ServiceType?.Description,
            s.Quantity,
            s.Cost,
            Options = (!string.IsNullOrEmpty(s.Options) ? s.Options.Split(',') : null),
            Total = s.Quantity * s.Cost
        }),
        Success = true
    };
var json = JsonConvert.SerializeObject(result);
return Content(json, "application/json");
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your answer, both methods fails too. The parser works, if I console or debbug the original version, is parsed correctly, the problem is when I add the deserialized object to the Json object, it always return "filter":[[[]],[[[],[]]]] ... the Json object is messing it up serializing it again, and if I left the filter field as it is (json string) the Json Object is sending it as key with the json as value
sorry I was wrong in something, is not webapi, is mvc, the same code as yours prnt.sc/iaywsi

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.