根据C#中的条件从JSON更新元素

时间:2012-11-22 08:40:13

标签: c# json json.net

我有一个JSON字符串,我希望能够在C#中修改。我希望能够在满足某个条件时更新结果中的值。

采取以下

 {
  "responseHeader":{
    "status":0,
    "QTime":0,
    "params":{
      "explainOther":"",
      "fl":"*,score",
      "indent":"on",
      "start":"0",
      "q":"*:*",
      "hl.fl":"",
      "qt":"",
      "wt":"json",
      "fq":"",
      "version":"2.2",
      "rows":"2"}
  },
  "response":{"numFound":2,"start":0,"maxScore":1.0,"docs":
  [{
        "id":"438500feb7714fbd9504a028883d2860",
        "name":"John",
        "email":"john@email.com"
        "dateTimeCreated":"2012-02-07T15:00:42Z",
        "dateTimeUploaded":"2012-08-09T15:30:57Z",
        "score":1.0
   },
   {
        "id":"2f7661ae3c7a42dd9f2eb1946262cd24",
        "name":"David",
        "email":"david@email.com"
        "dateTimeCreated":"2012-02-07T15:02:37Z",
        "dateTimeUploaded":"2012-08-09T15:45:06Z",
        "score":1.0
    }]
 }}

我希望能够在找到具有特定ID的结果时更新名称和电子邮件元素值。

例如,我想要更新文档ID等于的名称和电子邮件元素" 438500feb7714fbd9504a028883d2860"并将名称值更新为Richard,将电子邮件值更新为richard@email.com。结果如下所示。

 {
  "responseHeader":{
    "status":0,
    "QTime":0,
    "params":{
      "explainOther":"",
      "fl":"*,score",
      "indent":"on",
      "start":"0",
      "q":"*:*",
      "hl.fl":"",
      "qt":"",
      "wt":"json",
      "fq":"",
      "version":"2.2",
      "rows":"2"}
  },
  "response":{"numFound":2,"start":0,"maxScore":1.0,"docs":
  [{
        "id":"438500feb7714fbd9504a028883d2860",
        "name":"Richard",
        "email":"richard@email.com"
        "dateTimeCreated":"2012-02-07T15:00:42Z",
        "dateTimeUploaded":"2012-08-09T15:30:57Z",
        "score":1.0
   },
   {
        "id":"2f7661ae3c7a42dd9f2eb1946262cd24",
        "name":"David",
        "email":"david@email.com"
        "dateTimeCreated":"2012-02-07T15:02:37Z",
        "dateTimeUploaded":"2012-08-09T15:45:06Z",
        "score":1.0
    }]
 }}

性能是一个考虑因素,因为我需要处理许多JSON字符串,所以请记住这一点。

提前致谢 安德鲁

2 个答案:

答案 0 :(得分:0)

您必须使用DataContractJsonSerializer解码JSONstring。 (Here you can find一篇非常好的博客文章,讨论如何使用它。)

之后,您可以更改所需的所有值,最后在JSON中重新设置所有内容

答案 1 :(得分:0)

您可以对此http://json.codeplex.com/

使用“Newtonsoft.Json”

它非常易于使用!我写了一个小例子,它完全符合你的要求:

测试方法: [C#]

    private void Test(){
        byte[] Bytes = File.ReadAllBytes("json.txt");
        string Json = Encoding.ASCII.GetString(Bytes);

        Response JsonResponse = JsonConvert.DeserializeObject<Response>(Json);

        if (JsonResponse != null){
            if (JsonResponse.response != null){
                foreach(Document Doc in JsonResponse.response.docs){
                    if (Doc.id == "2f7661ae3c7a42dd9f2eb1946262cd24"){
                        Doc.name = "David";
                        Doc.email = "david@email.com";
                    }
                }

                string JsonMod = JsonConvert.SerializeObject(JsonResponse, Formatting.Indented);

                BinaryWriter Writer = new BinaryWriter(File.Create("JsonMod.txt"));
                Writer.Write(Encoding.ASCII.GetBytes(JsonMod));
                Writer.Close();
                Writer.Dispose();
            }
        }
    }

Json课程: [C#]

[JsonObject(MemberSerialization.OptIn)]
public class Response{

    [JsonProperty]
    public ResponseHeader responseHeader{
        get; set;
    }

    [JsonProperty]
    public ResponseContent response{
        get; set;
    }

}

[JsonObject(MemberSerialization.OptIn)]
public class ResponseHeader{
    [JsonProperty]
    public int Status{
        get; set;
    }

    [JsonProperty]
    public int QTime{
        get; set;
    }

    [JsonProperty]
    public object Params{
        get; set;
    }
}

[JsonObject(MemberSerialization.OptIn)]
public class ResponseContent{

    [JsonProperty]
    public int numFound{
        get; set;
    }

    [JsonProperty]
    public int start{
        get; set;
    }

    [JsonProperty]
    public double maxScore{
        get; set;
    }

    [JsonProperty]
    public Document[] docs{
        get; set;
    }
}

[JsonObject(MemberSerialization.OptIn)]
public class Document{

    [JsonProperty]
    public string id{
        get; set;
    }

    [JsonProperty]
    public string name{
        get; set;
    }

    [JsonProperty]
    public string email{
        get; set;
    }

    [JsonProperty]
    public DateTime dateTimeCreated{
        get; set;
    }

    [JsonProperty]
    public DateTime dateTimeUploaded{
        get; set;
    }

    [JsonProperty]
    public double score{
        get; set;
    }
}

哦,请查看你发布的json - 在“email”之后:“x@y.de”你错过了一个“,” - 这将导致解析异常;)

干杯!