0

when use JsonConvert.DeserializeObject(str) to parse a json string, the playerInfo parsed as string, how to remove the " in the start and the end of playerInfo so it can be parsed as object? enter image description here

below link is the full json text

Mohammad Aghazadeh
  • 2,108
  • 3
  • 9
  • 20
sun yu
  • 9
  • 1
  • Can't you fix the code which created this JSON? – Klaus Gütter Aug 06 '23 at 07:43
  • no I don't own the Json creation code – sun yu Aug 06 '23 at 07:47
  • 1
    Not just the start and end quotes, you would also need to unescape the internal quotes – Hans Kesting Aug 06 '23 at 08:19
  • 3
    Maybe a two-step process: 1) deserialize as-is, keeping playerInfo as string; 2) deserialize that playerInfo – Hans Kesting Aug 06 '23 at 08:27
  • 2
    How about deserializing it like you did for the surrounding JSON? `JsonConvert.DeserializeObject(obj.playerInfo)` – Xerillio Aug 06 '23 at 08:28
  • 1
    Assuming that `"playerInfo"` corresponds to some property `public PlayerInfo playerInfo { get; set; }` in your model, you could apply `[JsonConverter(typeof(EmbeddedLiteralConverter))]` from [this answer](https://stackoverflow.com/a/39154630/3744182) to [How do I convert an escaped JSON string within a JSON object?](https://stackoverflow.com/q/39154043) or `[JsonConverter(typeof(MyConverter))]` from [this answer](https://stackoverflow.com/a/64640742) to [Deserializing stringified (quote enclosed) nested objects with Newtonsoft Json.Net](https://stackoverflow.com/q/64640265). – dbc Aug 06 '23 at 17:21
  • Do those two answers answer your question also? If not, please [edit] your question to share minimal versions of your JSON and data model **as text, in the question itself** rather than as a screen shot + external link. For why, see [*Why should I not upload images of code/data/errors?*](https://meta.stackoverflow.com/a/285557) and [Something in my web site or project doesn't work. Can I just paste a link to it?](https://meta.stackoverflow.com/q/254428). – dbc Aug 06 '23 at 17:23

1 Answers1

0

try this

JArray jArr = (JArray)JObject.Parse(str)["pointList"];

foreach (JObject jObj in jArr)
    foreach (var prop in jObj.Properties())
       if (prop.Value.Type == JTokenType.Object)
        if (prop.Value["playerInfo"] != null)
         prop.Value["playerInfo"] = JObject.Parse((string)prop.Value["playerInfo"]);
Serge
  • 40,935
  • 4
  • 18
  • 45