2012-11-22 33 views
1

我有一個JSON字符串,我希望能夠在C#中修改。我希望能夠在滿足特定條件時更新結果中的值。從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":"[email protected]" 
     "dateTimeCreated":"2012-02-07T15:00:42Z", 
     "dateTimeUploaded":"2012-08-09T15:30:57Z", 
     "score":1.0 
    }, 
    { 
     "id":"2f7661ae3c7a42dd9f2eb1946262cd24", 
     "name":"David", 
     "email":"[email protected]" 
     "dateTimeCreated":"2012-02-07T15:02:37Z", 
     "dateTimeUploaded":"2012-08-09T15:45:06Z", 
     "score":1.0 
    }] 
}} 

我希望能夠以更新的姓名和電子郵件元素值,當我找到一個包含特定ID的結果。

例如,我想要更新名稱和電子郵件元素,其中該文檔ID等於「438500feb7714fbd9504a028883d2860」,並將名稱值更新爲Richard並將電子郵件值更新爲[email protected]。結果如下所示。

{ 
    "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":"[email protected]" 
     "dateTimeCreated":"2012-02-07T15:00:42Z", 
     "dateTimeUploaded":"2012-08-09T15:30:57Z", 
     "score":1.0 
    }, 
    { 
     "id":"2f7661ae3c7a42dd9f2eb1946262cd24", 
     "name":"David", 
     "email":"[email protected]" 
     "dateTimeCreated":"2012-02-07T15:02:37Z", 
     "dateTimeUploaded":"2012-08-09T15:45:06Z", 
     "score":1.0 
    }] 
}} 

性能是一個考慮因素,因爲我需要處理許多JSON字符串,所以請牢記這一點。

在此先感謝 安德魯

+0

解碼JSON,更改值,序列化爲JSON。 –

+0

@Asad回答這個問題! –

回答

0

您可以使用「Newtonsoft.Json」這個http://json.codeplex.com/

這很容易使用!我寫了一個小例子究竟你想要的東西:

測試方法: [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 = "[email protected]"; 
        } 
       } 

       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」之後:「[email protected]」你錯過了一個「,」 - 這將導致解析異常;)

乾杯!

0

你必須THA JSONstring解碼,使用DataContractJsonSerializer。 (Here you can find一個非常好的博客文章,談論如何使用它)。

之後,你可以改變所有你想要的值,最後重新serilize的所有東西JSON