更新数组中JSON对象的数字属性

时间:2018-06-30 23:19:58

标签: c# json json.net

我的JSON config.json

[  
   {  
      "names":"Steam",
      "count": 1,
   },
   {  
      "names":"game",
      "count": 2,
   }
]

我需要在c#中更新数组中count的属性。 我尝试过

JObject objectproperty = JObject.Parse(File.ReadAllText(@"config.json"));

但这给了我错误

Newtonsoft.Json.JsonReaderException: "Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1."

1 个答案:

答案 0 :(得分:0)

您可以使用JToken
假设您的JSON为:
(注意,我必须删除一些<!DOCTYPE html> <html> <body> <script type = "text/javascript" src ="https://canvasjs.com/assets/script/canvasjs.min.js" > </script> <h2> Return of Investment Calculator </h2> <label> Initial Investment: </label> <input id="amount" value="500" min="1"> <label> Interest( % ): </label> <input id="ratePerMonth" value="3" min="1.0" Max="100.0"> <label> Months: </label> <input id="numberOfMonths" value="3"> <button onclick = "calculate()" > Calculate </button> <p id="display"> </p> <script type = "text/javascript" > function calculate() { //Beginning of user input values amount = document.getElementById("amount").value; //obtain initial investment ratePerMonth = document.getElementById("ratePerMonth").value; // rate increase per month numberOfMonths = document.getElementById("numberOfMonths").value; // no. of months //End of user input values // Calculator Down Below var investment = []; var text = ""; var month = new Date().getMonth(); var monthHolder = Number(month + 1); var total = amount; var additionalRate = 0; for (var i = -1; i < numberOfMonths; i++) { //Start of loop for compound switch (monthHolder) { //switching getMonth function to literal strings case 1: monthDisplay = "January"; break; case 2: monthDisplay = "February"; break; case 3: monthDisplay = "March"; break; case 4: monthDisplay = "April"; break; case 5: monthDisplay = "May"; break; case 6: monthDisplay = "June"; break; case 7: monthDisplay = "July"; break; case 8: monthDisplay = "August"; break; case 9: monthDisplay = "September"; break; case 10: monthDisplay = "October"; break; case 11: monthDisplay = "November"; break; case 12: monthDisplay = "December"; }; text += "Current Amount " + parseFloat(total).toFixed(2) + "Rate: " + ratePerMonth + "% Month: " + monthDisplay + "<br>"; //inserting data investment.push([ monthDisplay,total ]); //end of data insert additionalRate = total * ratePerMonth / 100; total = Number(additionalRate) + Number(total); if (monthHolder < 12) { monthHolder++; } else { monthHolder = 1; } } // chartInvestment.render(); document.getElementById("display").innerHTML = text; alert(investment[2][1]); var chart = new CanvasJS.Chart("chartContainer", { title: { text: "Investment Chart" }, data: [{ type: "line", datapoints: investment }] }); chart.render(); } </script> <div id = "chartContainer" style = "height: 300px; width: 100%;" ></div> </body> </html>

,

您可以使用此:

[  
   {  
      "names":"Steam",
      "count": 1
   },
   {  
      "names":"game",
      "count": 2
   }
]