2016-07-27 4 views
0

ich eine JSON-Datei haben, die etwas wie folgt aussieht: Jetzt-Update eine JSON-Datei mit einem Modellobjekt in 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" 
    }, 
    .... 

, habe ich ein Objekt, das wie folgt aussieht:

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

Und es hat dementsprechend ein Wert:

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

Was will ich ist ..

Ich mag die JSON-Datei lesen und wenn irgendein picklist_typ_key des Eintrags entspricht die eingehende Objekt groupKey, mag ich nur das Objekt des picklist_typ_cd mit groupCode und picklist_typ_dsc mit groupName aktualisieren.

Ich konnte die Daten von JSON als ...

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

Bitte helfen Sie mir, es zu tun lesen.

+0

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

+0

@Venky .. wahrscheinlich, Sie haben es nicht gelesen. Es geht um selektive Aktualisierung und die Logik für dasselbe. – StrugglingCoder

+0

Ok. Dann http://stackoverflow.com/questions/21695185/change-values-in-json-file-writing-files – Venky

Antwort

0

Können Sie die LINQ "All" -Kette versuchen? Etwas wie folgt aus:

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

: 
: 

someFunction(PicklistData myPick) { 
: 
} 
Verwandte Themen