如何将扁平JSON转为动态对象并添加Total属性?
Hey there! Let's break down how to solve this problem exactly as you need it—no predefined classes, just dynamic objects that let you add the Total property after converting the JSON string.
First, let's restate your requirements clearly for context:
I have a simple object saved as a JSON string, and I want to convert it back to an object while adding a new property.
JSON string:
{"Database":0,"SalesForce":0,"Watson":0,"AIC":0,"Luise":0}Property to add:
"Total":0The final object structure should look like (conceptually):
class myObj{ private int Database; private int SalesForce; private int Watson; private int AIC; private int Luise; private int Total; }I don't have a predefined entity class and want to implement this using a dynamic object.
C# Solutions
C# has two common libraries for JSON handling—here's how to do it with both:
Using Newtonsoft.Json (Json.NET)
This is the most widely used library for dynamic JSON handling in C#:
using Newtonsoft.Json; // Your input JSON string string jsonInput = @"{""Database"":0,""SalesForce"":0,""Watson"":0,""AIC"":0,""Luise"":0}"; // Deserialize to a dynamic object dynamic dynamicObject = JsonConvert.DeserializeObject(jsonInput); // Add the new Total property dynamicObject.Total = 0; // Optional: Serialize back to JSON to verify the result string updatedJson = JsonConvert.SerializeObject(dynamicObject); Console.WriteLine(updatedJson);
Using System.Text.Json (.NET 6+)
If you prefer the built-in .NET library, use JsonObject for dynamic manipulation:
using System.Text.Json; using System.Text.Json.Nodes; string jsonInput = @"{""Database"":0,""SalesForce"":0,""Watson"":0,""AIC"":0,""Luise"":0}"; // Deserialize to a JsonObject (dynamic structure) JsonObject dynamicObject = JsonSerializer.Deserialize<JsonObject>(jsonInput)!; // Add the Total property dynamicObject["Total"] = 0; // Verify the result string updatedJson = JsonSerializer.Serialize(dynamicObject); Console.WriteLine(updatedJson);
Python Solution
Python's JSON module natively converts JSON to dictionaries, which are inherently dynamic:
import json # Your input JSON string json_input = '{"Database":0,"SalesForce":0,"Watson":0,"AIC":0,"Luise":0}' # Convert JSON to a dynamic dictionary dynamic_obj = json.loads(json_input) # Add the Total key-value pair dynamic_obj["Total"] = 0 # Verify the result print(json.dumps(dynamic_obj, indent=2))
JavaScript Solution
JavaScript makes this extremely straightforward since JSON.parse returns a plain object that you can modify directly:
// Your input JSON string const jsonStr = '{"Database":0,"SalesForce":0,"Watson":0,"AIC":0,"Luise":0}'; // Parse JSON to a dynamic object const dynamicObj = JSON.parse(jsonStr); // Add the Total property dynamicObj.Total = 0; // Verify the result console.log(JSON.stringify(dynamicObj, null, 2));
All these solutions avoid defining a static entity class and let you dynamically add the Total property after deserializing the JSON.
内容的提问来源于stack exchange,提问作者Bill Software Engineer

