处理json文件csharp控制台

时间:2017-07-31 13:30:50

标签: c# json json.net

我有本地Json文件,我想处理它。所以删除,创建    ,改变价值,并获得。

所以,这是我的json示例

[   {
   "Alerts": {
     "Name": "RoseDeal",
     "Description": "Problème au sein de Rosedeal",
     "StartDate": "2017-07-17T00:00:00",
     "EndDate": "2008-07-19T00:00:00",
     "Niveau": "Urgent",
     "Type": "Girophare",
     "Id": 1
   }   } ]

我在一个控制台程序中工作只是为了尝试。

public bool DeleleAlert(int alertId )
{                 
    var filePath = @"C:\Users\korben\Documents\Visual Studio       2017\Projects\SaveFiles\SaveFiles\json\alerts.json";
    string Json = File.ReadAllText(filePath);
    var alerts = JsonConvert.DeserializeObject<List<Alert>>(Json);
    var newJsonString = JsonConvert.SerializeObject(alerts.Where(i =>   i.Id != alertId));
    return true;
}


public bool SaveAlert(Alert newAlert)
{
    var filePath = @"C:\Users\korben\Documents\Visual Studio 2017\Projects\SaveFiles\SaveFiles\json\alerts.json";
    string Json = File.ReadAllText(filePath);
    dynamic contourManifest = JObject.Parse(Json);
    foreach (var Alerts in contourManifest.features)
    {
      Alerts.Name.Replace( JObject.FromObject(new { Name = Alerts.Name["SANG"] }));
    }

    var newJson = contourManifest.ToString();

// Missing a closing brace here?

public Alert GetAlert(int alertId)
{
   List<Alert> alerts;
   // Lire le fichier JSON 
   alerts = ReadFile();
   // Rechercher l'alerte demandé via sont ID
   var myAlert = alerts.Where(a => a.Id == alertId).FirstOrDefault();
   return myAlert;
}

} } // <-- ?

public void CreateALert(List<AlertObject> alerts)
{
      var filePath = @"C:\Users\korben\Documents\Visual Studio             2017\Projects\SaveFiles\SaveFiles\json\alerts.json";
      string json = JsonConvert.SerializeObject(alerts, Formatting.Indented);
      File.WriteAllText(filePath, json); 
}
} // <-- ?

它不是我的所有代码,只是创建,GetAlert工作。删除和 savealert(变更值)不工作,我不明白为什么。 这是程序:

static void Main(string[] args)
{
       int count = 0;
       var myAlert = new List<Alert>();
       var myAlertObject = new List<AlertObject>();
       AlertObject oAlertObject = new AlertObject();
       var newAlert = new Alert
       {
           Name = "RoseDeal",
           Description = "Problème au sein de Rosedeal",
           StartDate = new DateTime(2017, 7, 17),
           EndDate = new DateTime(2008, 7, 19),
           Niveau = "Urgent",
           Type = "Girophare",
           Id = ++count,
       };

       oAlertObject.OAlert = newAlert;
       myAlertObject.Add(oAlertObject);
       Console.WriteLine("Hello World!");
       JsonAlert rene = new JsonAlert();
       rene.CreateALert(myAlertObject);
       rene.GetAlert(1);
       //rene.SaveAlert(newAlert);
       rene.DeleleAlert(1);
 }
 } // <-- ?

1 个答案:

答案 0 :(得分:0)

您可以这样处理:

public class Alert //This is each alert
{
    public string Name;
    public string Decription;
    ...
}

public class Alerts //This is group of all alert
{
    public List<Alert> Alert;

    public Alerts()
    {
        Alert = new List<Alert>();
    }
}

public class Program
{        
    public static Alerts LoadAlertsFromJson() //With this you can load alerts to other classes with Program.LoadAlertsFromJson();
    {
        string jsonString = loadYourStringHere;
        Alerts a = JsonUtility.FromJson<Alerts>(jsonString);
        return a;
    }

    public static void AddAlert(Alert alert)
    {
        Alerts alerts = LoadAlertsFromJson();
        alerts.Add(alert);
        SaveAlerts(alerts);
    }

    public static void SaveAlerts(Alerts alerts)
    {
        string jsonString = JsonUtility.ToJson(alerts);
        //write this string to your file
    }
}