Replacing only a specific part of a JSON object

Good. I am working on a save editor for the game as a project, and save is in JSON. I have a small snippet returned from the actual editor after using it, and it looks like this:

{"hero":{"STR":10,"HP":33,"HT":35,"attackSkill"13,"defenseSkill":3}}

This is only a small part of the conservation. I can’t understand in my whole life how to replace this part in all salvation. I tried

  • Json to C # is all that. This does not work, because for some reason the game developer decided that half of the fields would be the name of the class from his game, for example. "Com.whatever.whatever": 1. This is a distracting parser.
  • Just using the returned snippet. The game will not accept it, because it is not as complete as the actual save.
  • Just a search for values. I can't get this to work, simply because I don't know how to properly manipulate strings.

I want to just replace

{"hero"{...}} 

part with a new part.

How can I do it? Thank.

In addition, the pasty game saves if someone wants / needs it: http://pastebin.com/eK13cNtg

+4
source share
3 answers

Json.NET supports "LINQ to JSON" and dynamic access, which allows you to process the entire tree dynamically without the need for POCO mapping. This has the advantage over deserializing typed objects in that additions to matching do not need to be considered for serialization with rounding.

JSON, JSON.

: . ( JSON - eww, btw! - , .)

JObject data = JObject.Parse(json);
JObject hero = (JObject)data["hero"];
hero["STR"] = 42;        // "god-like strength" hax; modifies the JObject
json = data.ToString();  // re-serialize tree, along with modifications

dynamic type Json.NET.


Json.NET " ", , Dictionary/JObject.

POCO - - / ; , , , "" POCO.

+7

#, , . , , ( JSON, ), , JSON ( Javascript).

Javascript

var myObj = {'no.kidding':'This is valid'};
alert(myObj['no.kidding']);

, " ".

, , , , .

: http://json.org/ - #, , , .

(, , ), , , '.' '_' ( - ) , , . - , , .

+2

Json to C # is all that. This does not work, because for some the reason the game developer decided that half of the fields would be the class names from his game, for example. "Com.whatever.whatever": 1. This is an analyzer concern.

I could not recreate the problem. This works for me.

dynamic response = Newtonsoft.Json.Linq.JObject.Parse(json);
var jsonNewHero = "{'hero':{'STR':10,'HP':33,'HT':35,'attackSkill': 13,'defenseSkill':3}}";
dynamic newHero = Newtonsoft.Json.Linq.JObject.Parse(jsonNewHero);
response.hero = newHero.hero;
var newJson = Newtonsoft.Json.JsonConvert.SerializeObject(response, Newtonsoft.Json.Formatting.Indented);

Work .NET Fiddle: http://dotnetfiddle.net/PKPQC1

PS: Using the latest version of Newtonsoft Json version 6.0.1

+2
source

Source: https://habr.com/ru/post/1529621/


All Articles