简体   繁体   中英

convert c# string[] to json

I have a method that returns a string[] back to a controller action and that action returns a JsonAction like so:

// GET: Locations/Get
[HttpGet]
public async Task<JsonResult> Get(string lotNumber)
{
    var result = await this.MakeApiCall("/locations?lotNumber=" + lotNumber);

    return Json(result, JsonRequestBehavior.AllowGet);
}

in my Javascript, if I do this:

core.get(locationsApi + "?lotNumber=" + core.getParameterByName("lotNumber"), function (response) {
    var r = JSON.parse(response);

    console.log(r);
});

I see a string that looks similar to this:

["BA01A1","BA01A2","BA01A3"]

but, this seems to be a string because if I do this:

core.get(locationsApi + "?lotNumber=" + core.getParameterByName("lotNumber"), function (response) {
    var r = JSON.parse(response);
    var r = JSON.parse(r);

    console.log(r);
});

It actually returns an array.... How can I get my action to return the array in the beginning or at the very least return something that I only have to parse once?

It sounds like the type of var result = returned from MakeApiCall is of type string but contains ["BA01A1","BA01A2","BA01A3"] which is essentially serialized JSON.

You can determine this by mousing over var and seeing what the type is, and also using a debugger to see what the actual value is returned from the API call.

When you then return that serialized JSON with Json(result, JsonRequestBehavior.AllowGet); the string is getting serialized into yet another JSON representation.

Essentially it's been double serialized.

When you use this, client side the result.length == 3, the expected length of the array.

    public ActionResult GetAnswer(string question)
    {               
        var answer = @"[""BA01A1"",""BA01A2"",""BA01A3""]";
        return Content(answer, "text/javascript");
    }
    // javascript result.length returns 3

When instead you use the Json helper, you get a string with 28 characters instead, because the Json helper assumes you are passing an object that should be serialized, but the problem is, you already have a an array that's represented as a serialized string:

    public ActionResult GetAnswer(string question)
    {                   
        var answer = @"[""BA01A1"",""BA01A2"",""BA01A3""]";
        return Json(answer);            
    }
    // javascript result.length returns 28

This fiddle demonstrates this.

https://dotnetfiddle.net/NCAPkh

TLDR In your case var result is likely already a string representation of a JSON serialized object, and passing it to the Json( helper then serializes it again. Hence client side you must deserialize twice.

You have two options, either deserialize the string from the API call into a C# object list, then pass that object list to return Json(someObject... or return it as Content with type JSON(demonstrated above).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM