0

我有一些 JSON 存储在一个文件中,如下所示。我想要做的是找到Task2,将其状态从completeto更改deleted,然后将其保存回我从中获取的文本文件中。但是对于我的一生,我无法弄清楚这一点。

这是我的 JSON:

{
    "tasks": [
        {
            "task"     : "Task1",
            "quantity" : "(1)",
            "state"    : "incomplete",
            "changed"  : "never"    
        },
        {
            "task"     : "Task2",
            "quantity" : "(1)",                             
            "state"    : "complete",
            "changed"  : "never"                    
        },
        {
            "task"     : "Task3",
            "quantity" : "(1)",             
            "state"    : "deleted",
            "changed"  : "never"                    
        }
    ]
}

这是我到目前为止的代码(使用 Json.Net)

using (StreamReader reader = new StreamReader(fileStream))
{
    string txt = reader.ReadToEnd();

    JObject jObject = JObject.Parse(txt);
    JArray tasks = (JArray) jObject["tasks"];

    ....

    reader.Close();
}

为了更新 JSON,我需要什么代码?

4

2 回答 2

0

你可以像这样很容易地完成你想要的:

// Read the JSON file 
string json = File.ReadAllText(fileName);

// Parse the JSON
JObject jObject = JObject.Parse(json);
JArray tasks = (JArray)jObject["tasks"];

// Find the first JObject in tasks having a "task" property with a value of "Task2"
// If such an object exists, set its "state" property to "deleted"
JObject task2 = (JObject)tasks.FirstOrDefault(a => a["task"].ToString() == "Task2");
if (task2 != null)
{
    task2["state"] = "deleted";
}

// Write the JSON back to the file
File.WriteAllText(fileName, jObject.ToString());

请注意,您需要using System.Linq;在代码顶部包含才能访问该FirstOrDefault()方法。

于 2014-12-21T22:52:46.767 回答
0

您可以使用 Json.Net (http://json.codeplex.com/releases/view/89222) 并将其引用添加到您的项目中,然后您可以执行类似的操作...

 [DataContract]
        public class TaskClass
        {
            [DataMember(Name="task")]
            public string task { get; set; }

            [DataMember(Name="quantity")]
            public string quantity { get; set; }

            [DataMember(Name="state")]
            public string state { get; set; }

            [DataMember(Name = "changed")]
            public string changed { get; set; }
        }

        [DataContract]
        public class Tasks
        {
            [DataMember(Name = "tasks")]
            public IEnumerable<TaskClass> tasks { get; set; }
        }

//and you can deserialize json :

    string json = "{\"tasks\":[{\"task\": \"Task1\",\"quantity\": \"(1)\",\"state\": \"incomplete\",\"changed\": \"never\"},{\"task\": \"Task2\",\"quantity\": \"(1)\",\"state\": \"complete\",\"changed\": \"never\"},{\"task\":\"Task3\",\"quantity\": \"(1)\",\"state\": \"deleted\",\"changed\": \"never\" }]}";

            Tasks allTasks = new Tasks();
            allTasks = JsonConvert.DeserializeObject<Tasks>(json);

            foreach (TaskClass task in allTasks.tasks)
            {
                if (task.task.ToLower() == "task2")
                {
                    task.state = "deleted";
                }
            }

//and serialize it again as :

string value = JsonConvert.SerializeObject(objTasks);
于 2012-07-16T09:44:34.407 回答