-1

I'm trying to deserialize this json:

{"name":"FC Internazionale Milano","y":55},{"name":"AC Chievo Verona","y":45,"sliced":true}

which is available inside that function that's contained in a string:

window.addEvent('domready', function() {
var chart = new PieChart('Possession');
     chart.addSeries('Possession', [{"name":"FC Internazionale Milano","y":55},{"name":"AC Chievo Verona","y":45,"sliced":true}]);

chart.highChartsOptions.plotOptions.pie.topmargin = '20';
chart.highChartsOptions.plotOptions.pie.size = "80%";
chart.highChartsOptions.plotOptions.pie.center = ['50%', '55%'];
    chart.render('page_chart_1_chart_statsplus_1_chart_possession_1-wrapper');

});

for the get json I used this regex:

System.Text.RegularExpressions.Regex.Match(script, @"Possession[^\{]+(.*})").Groups[1].Value;

which return the json above. So I deserialized in this way:

var json = JsonConvert.DeserializeObject<JsonMatchStat>(jsonStr);

the structure of JsonMatchStat is this:

 public class JsonMatchStat
 {
    public string name { get; set; }
    public int y { get; set; }
    public bool? sliced { get; set; }
 }

when I deserialize the json I get:

'Additional text encountered after finished reading JSON content: ,. Path '', line 1, position 42.'

what I did wrong?

fantaghiro
  • 93
  • 1
  • 8
  • 2
    This is not valid JSON, it looks like multiple JSON files stitched together with commas. – DavidG Apr 24 '18 at 13:58
  • I'm no JSON expert but there are two classes in that JSON string. Is this the issue? – spodger Apr 24 '18 at 13:59
  • those are two objects you want to deserialize as one, make it a list like [{"name":"FC Internazionale Milano","y":55},{"name":"AC Chievo Verona","y":45,"sliced":true}] and deserialize to a list instead var json = JsonConvert.DeserializeObject>(jsonStr); – Adeoluwa Simeon Apr 24 '18 at 14:00
  • @fantaghiro, it seems you already have an answer – Adeoluwa Simeon Apr 24 '18 at 14:13
  • Actually, reading a comma-delimited sequence of JSON objects is supported natively in Json.NET [11.0.1](https://github.com/JamesNK/Newtonsoft.Json/releases/tag/11.0.1). See [How to deserialize dodgy JSON (with improperly quoted strings, and missing brackets)?](https://stackoverflow.com/q/46788778/3744182). You need to set `JsonReader.SupportMultipleContent = true`. – dbc Apr 24 '18 at 19:46

1 Answers1

-1

The JSON you have extracted isn't valid, it's multiple JSON objects stitched together with commas. The problem is that you have missed off the surrounding [ and ] brackets that indicate the JSON is an array. So instead if you have Regex to include them (note: I'm no Regex expert, but this should work):

Possession[^\[]+(.*}\])

Now you can deserialise to an enumerable:

var result = JsonConvert.DeserializeObject<IEnumerable<JsonMatchStat>>(jsonStr);
DavidG
  • 113,891
  • 12
  • 217
  • 223