1

I have a JSON string that I want to be able to amend in C#. I want to be able to update a value in the results when I meet a certain condition.

Take the following

 {
  "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
    }]
 }}

I want to be able to update the name and email element values when I find a result with a certain Id.

For example I would want to update the name and email element where that docs Id equals "438500feb7714fbd9504a028883d2860" and update the name value to Richard and the email value to richard@email.com. The result of this is shown below.

 {
  "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
    }]
 }}

Performance is a consideration as I will need to process many JSON strings, so please bear that in mind.

Thanks in advance Andrew

4

2 に答える 2

0

を使用して、JSON文字列をデコードする必要がありますDataContractJsonSerializer。(ここでは、それを使用する方法について話している本当に良いブログ投稿を見つけることができます)。

その後、必要なすべての値を変更し、最後に、JSONですべてのものを再セリライズできます

于 2012-11-22T08:49:20.457 に答える
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 に答える