2016-07-27 8 views
0

C#でのモデルオブジェクトとJSONファイル:それはあり更新私のようなものに見えるJSONファイル持って

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;} 
} 

そして:今

[ 
    { 
    "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" 
    }, 
    .... 

を、私はのように見えるオブジェクトを持っていますそれに応じて値:私はあるにしたい何

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

..

私はJSONファイルを読みたいと任意のエントリのpicklist_typ_keyが入ってくるオブジェクトのgroupKeyが一致した場合、私は、そのオブジェクトのpicklist_typ_cdgroupNamegroupCodepicklist_typ_dscとを更新したいです。

私は私はそれを行う助けてください...

 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** 
      } 

としてJSONからデータを読み取ることができます。

+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) { 
: 
} 
関連する問題