2016-07-27 68 views
0

我有一個JSON文件,看起來像一個JSON文件:更新,在C#中的模型對象

[ 
    { 
    "picklist_typ": "Address Assessment Code", 
    "picklist_typ_key": null, 
    "picklist_typ_cd": "DELIVERABLE", 
    "picklist_typ_dsc": "Address is deliverable : Deliverable", 
    "ref_order": null, 
    "dw_trans_ts": "2016-07-17T12:59:15" 
    }, 
    { 
    "picklist_typ": "Address Assessment Code", 
    "picklist_typ_key": null, 
    "picklist_typ_cd": "NOT-DELIVERABLE", 
    "picklist_typ_dsc": "Address is not deliverable : Undeliverable", 
    "ref_order": null, 
    "dw_trans_ts": "2016-07-17T12:59:15" 
    }, 
    { 
    "picklist_typ": "Address Type", 
    "picklist_typ_key": null, 
    "picklist_typ_cd": "B", 
    "picklist_typ_dsc": "Billing Address", 
    "ref_order": null, 
    "dw_trans_ts": "2016-07-17T12:59:15" 
    }, 
    .... 

現在,我有一個對象,看起來像:

public class GroupMembershipWriteOutput 
{ 
    public long? groupKey { get; set; } 
    public string transOutput { get; set; } 
    public long? transKey { get; set; } 
    public string groupCode {get;set;} 
    public string groupName {get;set;} 
} 

而且它有因此值:

groupKey:121 
    transOutput: "Success" 
    transKey:998546 
    groupCode:"My Group Test" 
    groupName: "My Created Group Test" 

我想什麼是..

我想讀的JSON文件,如果任何條目的picklist_typ_key匹配傳入對象的groupKey,我只想更新該對象的picklist_typ_cdgroupCodepicklist_typ_dscgroupName

我可以從JSON作爲讀取數據...

 if(gmwo.transOutput.ToUpper() == "SUCCESS") 
     { 
      string json = System.Configuration.ConfigurationManager.AppSettings["PicklistDataPath"]; 
      List<PicklistData> deserializedPicklistData = JsonConvert.DeserializeObject<List<PicklistData>>(System.IO.File.ReadAllText(json)); 

      //find the object that matches 

      IEnumerable<PicklistData> results = deserializedPicklistData.Where(item => item.picklist_typ_key == gmwo.groupKey.ToString()); 

      if(results != null) 
      { 
       **//What is the logic to update only that entry in the json file** 
      } 

請幫我做到這一點。

+0

http://stackoverflow.com/questions/16921652/how-to-write-a-json-file-in-c – Venky

+0

@Venky ..也許,你沒有看過它。它是關於選擇性更新和相同的邏輯。 – StrugglingCoder

+0

好的。然後http://stackoverflow.com/questions/21695185/change-values-in-json-file-writing-files – Venky

回答

0

你可以試試LINQ「全部」鏈嗎?事情是這樣的:

IEnumerable<PicklistData> results = deserializedPicklistData 
    .Where(item => item.picklist_typ_key == gmwo.groupKey.ToString()) 
    .All(selectedItem => someFunction(selectedItem)); 

: 
: 

someFunction(PicklistData myPick) { 
: 
}