更改特定属性

时间:2017-02-28 07:13:36

标签: c# json postman

如果[@"your Text" drawInRect:CGRectMake(0.f, 0.f, 320.f, 80.f) withAttributes:"dictionary with text attributes"]; CourseName同等,如何将HistoryGym更改为ID

2

下面的代码只是一个幻想代码,以显示我的想法:

{
  "Result": {
    "StudentInfo": {
      "ID": 20,
      "Name": "Bob",
      "IgnoreThis": [
        {
          "ID": 123,
          "Something":"abc"
        }
      ]
    },
    "Courses": [
      {
        "ID": 1,
        "CourseName":"Math"
      },
      {
        "ID": 2,
        "CourseName":"History"
      }
    ]
  }
}

我将使用Postman

1 个答案:

答案 0 :(得分:0)

这是一个有效的解决方案(它可以整理);然而它使用Newtonsoft.Json(一个众所周知的nuget包)。

using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string blah = "{'Result': { 'StudentInfo': { 'ID': 20, 'Name': 'Bob', 'IgnoreThis': [{'ID': 123,'Something':'abc'}]}, 'Courses': [{'ID': 1,'CourseName':'Math'},{'ID': 2,'CourseName':'History'}]}}";

            dynamic json = JsonConvert.DeserializeObject(blah);
            var dynamicJson = json.Result.Courses; // included to show how dynamic could be accessed instead

            JObject arr = json;

            foreach (var course in arr["Result"]["Courses"].Where(x => x["ID"].Value<int>() == 2))
            {
                course["CourseName"] = "Gym";
            }

            var newResult = json.ToString();
        }
    }
}