How to create JSON from strings without creating a custom class using JSON.Net library

前端 未结 3 2071
日久生厌
日久生厌 2021-01-14 16:58

I have this very simple method as below:

//my current file
using Newtonsoft.Json;
string key1 = \"FirstKey\";
string key2 = \"SecondKey\";
string key3 = \"Th         


        
3条回答
  •  终归单人心
    2021-01-14 17:08

    As mentioned in the comments, you could just as easily have created it using anonymous objects.

    private string CreateJson(string val1, string val2, string val3, string val4, string val5, string val6) {
    
        var configs = new[]
        { 
            new { FirstKey = val1, SecondKey = val2, ThirdKey = val3}, 
            new { FirstKey = val4, SecondKey = val5, ThirdKey = val6}
        };
    
        var jsonData = JsonConvert.SerializeObject(configs);
    
        return jsonData;
    }
    

提交回复
热议问题