Reputation: 43
I have this code. How do I change the values of the JSON objects in code.
"externalReference": "Integrator Reference",
"proposalType": "Private Individual",
"dealer": {
"dealerID": 9344
},
"financeDetails": {
"type": "HP",
"cashDeposit": 111,
"settlement": 11,
"partExchange": 22,
"term": 72,
"apr": 12.9,
"loanamount": 0.0,
"distanceSelling": true,
"vehicleAsset": {
"type": "Used",
"class": "Car",
"source": "UK",
"purpose": "Social",
"vrm": "string",
"dateOfRegistration": "16-10-2020",
"manufacturer": "string",
"model": "string",
"derivative": "string",
"capCode": "ALGU21S164SDTA",
"CurrentMileage": 25000,
"vatRate": 20,
"netValue": 5000
},
"additionalAssets": [
{
"type": "MOT",
"netValue": 250,
"vatRate": 20,
"description": "string"
},
{
"type": "BCI_VAT",
"netValue": 350,
"vatRate": 5,
"description": "string"
}
]
},
I have this so far;
public static void CallRequest(string fieldName, string enteredFieldValue)
{
string sendProposalRequestSuccess = File.ReadAllText(sendProposalUnityJSONFile);
dynamic jsonObj = JsonConvert.DeserializeObject(sendProposalRequestSuccess);
jsonObj[fieldName] = enteredFieldValue;
string output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
File.WriteAllText(sendProposalUnityJSONFile, output);
}
Upvotes: 1
Views: 989
Reputation: 3700
You can use JObject
which has a method to parse json text you read from file to json objects. You can then access property using keys depending on the depth of the property.
1) JObject Parsing and Modification
string sendProposalRequestSuccess = File.ReadAllText("myjson.json");
JObject jsonObj = JObject.Parse(sendProposalRequestSuccess);
jsonObj["proposalType"] = "Private Company";
jsonObj["financeDetails"]["cashDeposit"] = 213;
string output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
File.WriteAllText("myjson2.json", output);
Demo Example:
https://dotnetfiddle.net/DZA5BI
2) Json to C# Classes:
Another approach is convert json
to C#
classes using this helpful online tool. You can then deserialize json object to main class MyJson
. This way you can access each property field and modify at your will then save json back to file.
This is pretty useful if you want to save to database or show in a table and several other applications.
public class MyJson
{
public string externalReference { get; set; }
public string proposalType { get; set; }
public Dealer dealer { get; set; }
public FinanceDetails financeDetails { get; set; }
}
public class AdditionalAsset
{
public string type { get; set; }
public int netValue { get; set; }
public int vatRate { get; set; }
public string description { get; set; }
}
public class Dealer
{
public int dealerID { get; set; }
}
public class FinanceDetails
{
public string type { get; set; }
public int cashDeposit { get; set; }
public int settlement { get; set; }
public int partExchange { get; set; }
public int term { get; set; }
public double apr { get; set; }
public double loanamount { get; set; }
public bool distanceSelling { get; set; }
public VehicleAsset vehicleAsset { get; set; }
public List<AdditionalAsset> additionalAssets { get; set; }
}
public class VehicleAsset
{
public string type { get; set; }
public string @class { get; set; }
public string source { get; set; }
public string purpose { get; set; }
public string vrm { get; set; }
public string dateOfRegistration { get; set; }
public string manufacturer { get; set; }
public string model { get; set; }
public string derivative { get; set; }
public string capCode { get; set; }
public int CurrentMileage { get; set; }
public int vatRate { get; set; }
public int netValue { get; set; }
}
Implementation:
string sendProposalRequestSuccess = File.ReadAllText("myjson.json");
var jsonObj = JsonConvert.DeserializeObject<MyJson>(sendProposalRequestSuccess);
jsonObj.financeDetails.cashDeposit = 231;
string output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
File.WriteAllText("myjson2.json", output);
Output:
Upvotes: 1