简体   繁体   中英

remove quotes in JSON array. Controller or JS?

I have the following dataset:

Sampled Target    
54      100   
53      100  
9       25   
63      100    

I am then concatenating these rows into comma separated result to produce a JSON using the following code in my MVC controller:

JArray secondChart = new JArray(new JObject(
                                           new JProperty("name", "sampled"),
                                           new JProperty("data", new JArray(string.Join(",", MyContext
                                                    .Select(p => p.SpecimenSampled))))
                                                    ),
                                                    new JObject(
                                           new JProperty("name", "remaining"),
                                           new JProperty("data", new JArray(string.Join(",", MyContext
                                                    .Select(p => (p.SpecimenTargets - p.SpecimenSampled)))))

It produces the following json:

[{
 "name": "sampled",
 "data": ["54,53,9,63"]
 },
 {
 "name": "remaining",
 "data": ["46,47,16,37"]
 }
 ]

While i would except:

[{
 "name": "sampled",
 "data": [54,53,9,63]
 },
 {
 "name": "remaining",
 "data": [46,47,16,37]
 }
 ]

In order to be consumed by my javascript and to display a chart.

I have tried to get rid of the double quote in the controller but didn't succeed because of the string.Join which merges all my rows into a comma separated string. I also tried at the javascript level but failed as well.

Thanks for your solution either in the controller or the javascript.

Sylvain

Thanks to @aluan-haddad

I was mistakenly preprocessing the concatenation which would be taken care directly by JSON.net, so i just had to update my controller;

JArray secondChart = new JArray(new JObject(
                                           new JProperty("name", "sampled"),
                                           new JProperty("data", new JArray(MyContext
                                                    .Select(p => p.SpecimenSampled)))
                                                    ),
                                                    new JObject(
                                           new JProperty("name", "remaining"),
                                           new JProperty("data", new JArray(MyContext
                                                    .Select(p => (p.SpecimenTargets - p.SpecimenSampled)))) 

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