0

I have a problem. I am receiving the following JSON:

{"Templates":[[{"Id":"30","FileName":"iu6JjcovBwC5LZyU2pRMtXG4l.png"}],[{"Id":"29","FileName":"nJlZFivBzow5fmbR9cxCGOjyH.png"}],[{"Id":"28","FileName":"REpsr0vMkQlhY4AqxcofI1Pan.png"}],[{"Id":"27","FileName":"HIi73dwvZ9Kq5s6yg0eMf4GSc.png"}],[{"Id":"26","FileName":"oSHbkmWxcpfK2G9Fjw6DYzeA8.png"}],[{"Id":"25","FileName":"hKSPDbLqmAiHzBfc1YZTR3X70.png"}],[{"Id":"24","FileName":"53Drk9ZzXHC6bSYavIA02mJRg.png"}],[{"Id":"23","FileName":"ezu82sdroLIYKGSOwP1mxktlF.png"}],[{"Id":"22","FileName":"Z2IyWOpf7GSuU9BvhTrxz3XJw.png"}],[{"Id":"21","FileName":"2rfvThXKE4WLb38cgO5t1IqUw.png"}],[{"Id":"20","FileName":"kjnHiUstO3LEoNW1aG7hMu2CI.png"}],[{"Id":"19","FileName":"rzREfOGthcvPkSdeJ7IgYT26M.png"}],[{"Id":"18","FileName":"sY6AwNBiETSO87fJDjKZU1MVa.png"}],[{"Id":"17","FileName":"OHFnyQtmxi01Iluvd6Mr52AZ3.png"}],[{"Id":"16","FileName":"MQdv5FGAhsDofEran4VBSkI0K.png"}],[{"Id":"15","FileName":"8eb9VfJpqKhPgEdl3SwzrvGBQ.png"}],[{"Id":"14","FileName":"fWDHOAmcMlTYKr6wk70LUCuZV.png"}],[{"Id":"13","FileName":"HNzYXLceV7dgw6vtbBURjC93J.png"}]],"Source":{"TemplateSource":"media/templates/"}}

So I use this code to parse everything:

var jObject = JObject.Parse(json);
var templatePropery = jObject["Templates"] as JArray;
List<Template> templateList = new List<Template>();

foreach (var property in templatePropery)
{
    List<Template> propertyList = new List<Template>();
    propertyList = JsonConvert.DeserializeObject<List<Template>>(property.ToString());
    templateList.AddRange(propertyList);
}

var sourcePropery = jObject["Source"];
foreach (var property in sourcePropery)
{
    string tempplateSource = JsonConvert.DeserializeObject<string>(property.ToString());
    App.TemplateSource = tempplateSource;
}

Now all the Templates are getting successfully parsed, but the app crashes on the following line:

string tempplateSource = JsonConvert.DeserializeObject<string>(property.ToString());

With the error:

Newtonsoft.Json.JsonReaderException: 'Additional text encountered after finished reading JSON content

What am I doing wrong?

A. Vreeswijk
  • 822
  • 1
  • 19
  • 57

3 Answers3

1

Using json2csharp, you can map your JSON to classes:

public class Source
{
    public string TemplateSource { get; set; }
}

public class RootObject
{
    public List<List<Template>> Templates { get; set; }
    public Source Source { get; set; }
}

public class Template
{
    public string Id { get; set; }
    public string FileName { get; set; }
}

Then you can simply deserialize like this:

var jsonString = "{\"Templates\":[[{\"Id\":\"30\",\"FileName\":\"iu6JjcovBwC5LZyU2pRMtXG4l.png\"}],[{\"Id\":\"29\",\"FileName\":\"nJlZFivBzow5fmbR9cxCGOjyH.png\"}],[{\"Id\":\"28\",\"FileName\":\"REpsr0vMkQlhY4AqxcofI1Pan.png\"}],[{\"Id\":\"27\",\"FileName\":\"HIi73dwvZ9Kq5s6yg0eMf4GSc.png\"}],[{\"Id\":\"26\",\"FileName\":\"oSHbkmWxcpfK2G9Fjw6DYzeA8.png\"}],[{\"Id\":\"25\",\"FileName\":\"hKSPDbLqmAiHzBfc1YZTR3X70.png\"}],[{\"Id\":\"24\",\"FileName\":\"53Drk9ZzXHC6bSYavIA02mJRg.png\"}],[{\"Id\":\"23\",\"FileName\":\"ezu82sdroLIYKGSOwP1mxktlF.png\"}],[{\"Id\":\"22\",\"FileName\":\"Z2IyWOpf7GSuU9BvhTrxz3XJw.png\"}],[{\"Id\":\"21\",\"FileName\":\"2rfvThXKE4WLb38cgO5t1IqUw.png\"}],[{\"Id\":\"20\",\"FileName\":\"kjnHiUstO3LEoNW1aG7hMu2CI.png\"}],[{\"Id\":\"19\",\"FileName\":\"rzREfOGthcvPkSdeJ7IgYT26M.png\"}],[{\"Id\":\"18\",\"FileName\":\"sY6AwNBiETSO87fJDjKZU1MVa.png\"}],[{\"Id\":\"17\",\"FileName\":\"OHFnyQtmxi01Iluvd6Mr52AZ3.png\"}],[{\"Id\":\"16\",\"FileName\":\"MQdv5FGAhsDofEran4VBSkI0K.png\"}],[{\"Id\":\"15\",\"FileName\":\"8eb9VfJpqKhPgEdl3SwzrvGBQ.png\"}],[{\"Id\":\"14\",\"FileName\":\"fWDHOAmcMlTYKr6wk70LUCuZV.png\"}],[{\"Id\":\"13\",\"FileName\":\"HNzYXLceV7dgw6vtbBURjC93J.png\"}]],\"Source\":{\"TemplateSource\":\"media / templates / \"}}";

var desrializedJson = JsonConvert.DeserializeObject<RootObject>(jsonString);
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
0

You can use this code instead:

var tempplateSource = property.First.Value<string>();

Because you are using a JToken you need to do it that way.

I would recommend to change your model to deserialize the whole thing in one call.

But that is beyond the scope of the question.

Jonathan Alfaro
  • 4,013
  • 3
  • 29
  • 32
  • It is quite likely that the first element of the source tree might not be TemplateSource. how can you be sure .First will get what you are looking for – Jawad Jan 05 '20 at 04:02
  • If you look the JSON he posted there is only ONE element in the Source object and that is the TemplateSource property.... – Jonathan Alfaro Jan 05 '20 at 04:04
0

You should not try to use DeserializeObject on partial json generated from ToString...

Anyway, here is a quick fix (note the cast to JObject before the loop):

var sourcePropery = (JObject)jObject["Source"];
foreach (var property in sourcePropery)
{
    //string tempplateSource = JsonConvert.DeserializeObject<string>(property.ToString());
    string tempplateSource = property.Value.Value<string>();
    App.TemplateSource = tempplateSource;
}
odalet
  • 1,389
  • 10
  • 18