Skip to main content
Stack Overflow is like an encyclopedia, so we prefer to omit these types of phrases. It is assumed that everyone here is trying to be helpful.
Source Link
Dharman
  • 33.9k
  • 27
  • 103
  • 156

You simply have to replace the value that you want to update. Since StringBuilder has a .Replace inbuilt method, you can implement that method.

 `JArray arr = JArray.Parse(jsonResult.ToString());
 foreach (JObject obj in arr.Children<JObject>())
  {
      foreach(JProperty singleProp in obj.Properties())
      {
        string name = singleProp.Name;
        string value = singleProp.Value.ToString();
                    
        if (name.ToString().Equals("field1")) //good practice
        {
          Int64 newID = 1234;
          jsonResult.Replace(value, newID.ToString());//replacing old value with new value and directly updates jsonResult
        }

      //not necesssary, explanation is given below
        var jsonElement = JsonSerializer.Deserialize<JsonElement>(jsonResult.ToString());
         result = JsonSerializer.Serialize(jsonElement, options);
       }
   }`

And for better formatting, I used JsonSerializer so that your output will look like json object rather than whole string without any lines.

` var options = new JsonSerializerOptions()
                {
                    WriteIndented = true
                };
  var result = ""
  while loop{
     jsonResult.Append(r.GetValue(0).ToString());
     (Above code)
  }
`

I hope this helps.

You simply have to replace the value that you want to update. Since StringBuilder has a .Replace inbuilt method, you can implement that method.

 `JArray arr = JArray.Parse(jsonResult.ToString());
 foreach (JObject obj in arr.Children<JObject>())
  {
      foreach(JProperty singleProp in obj.Properties())
      {
        string name = singleProp.Name;
        string value = singleProp.Value.ToString();
                    
        if (name.ToString().Equals("field1")) //good practice
        {
          Int64 newID = 1234;
          jsonResult.Replace(value, newID.ToString());//replacing old value with new value and directly updates jsonResult
        }

      //not necesssary, explanation is given below
        var jsonElement = JsonSerializer.Deserialize<JsonElement>(jsonResult.ToString());
         result = JsonSerializer.Serialize(jsonElement, options);
       }
   }`

And for better formatting, I used JsonSerializer so that your output will look like json object rather than whole string without any lines.

` var options = new JsonSerializerOptions()
                {
                    WriteIndented = true
                };
  var result = ""
  while loop{
     jsonResult.Append(r.GetValue(0).ToString());
     (Above code)
  }
`

I hope this helps.

You simply have to replace the value that you want to update. Since StringBuilder has a .Replace inbuilt method, you can implement that method.

 `JArray arr = JArray.Parse(jsonResult.ToString());
 foreach (JObject obj in arr.Children<JObject>())
  {
      foreach(JProperty singleProp in obj.Properties())
      {
        string name = singleProp.Name;
        string value = singleProp.Value.ToString();
                    
        if (name.ToString().Equals("field1")) //good practice
        {
          Int64 newID = 1234;
          jsonResult.Replace(value, newID.ToString());//replacing old value with new value and directly updates jsonResult
        }

      //not necesssary, explanation is given below
        var jsonElement = JsonSerializer.Deserialize<JsonElement>(jsonResult.ToString());
         result = JsonSerializer.Serialize(jsonElement, options);
       }
   }`

And for better formatting, I used JsonSerializer so that your output will look like json object rather than whole string without any lines.

` var options = new JsonSerializerOptions()
                {
                    WriteIndented = true
                };
  var result = ""
  while loop{
     jsonResult.Append(r.GetValue(0).ToString());
     (Above code)
  }
`
Source Link

You simply have to replace the value that you want to update. Since StringBuilder has a .Replace inbuilt method, you can implement that method.

 `JArray arr = JArray.Parse(jsonResult.ToString());
 foreach (JObject obj in arr.Children<JObject>())
  {
      foreach(JProperty singleProp in obj.Properties())
      {
        string name = singleProp.Name;
        string value = singleProp.Value.ToString();
                    
        if (name.ToString().Equals("field1")) //good practice
        {
          Int64 newID = 1234;
          jsonResult.Replace(value, newID.ToString());//replacing old value with new value and directly updates jsonResult
        }

      //not necesssary, explanation is given below
        var jsonElement = JsonSerializer.Deserialize<JsonElement>(jsonResult.ToString());
         result = JsonSerializer.Serialize(jsonElement, options);
       }
   }`

And for better formatting, I used JsonSerializer so that your output will look like json object rather than whole string without any lines.

` var options = new JsonSerializerOptions()
                {
                    WriteIndented = true
                };
  var result = ""
  while loop{
     jsonResult.Append(r.GetValue(0).ToString());
     (Above code)
  }
`

I hope this helps.