1

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

采取以下

 {
  "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 字符串,所以请记住这一点。

在此先感谢安德鲁

4

2 回答 2

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" 之后您错过了一个 "," - 这将导致解析异常;)

干杯!

于 2012-11-22T09:14:50.960 回答
0

您必须使用DataContractJsonSerializer. (在这里你可以找到一篇非常好的博客文章,讨论如何使用它)。

之后,您可以更改所有想要的值,最后,重新序列化 JSON 中的所有内容

于 2012-11-22T08:49:20.457 回答