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

前端 未结 3 2068
日久生厌
日久生厌 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;
    }
    
    0 讨论(0)
  • 2021-01-14 17:11

    Getting some clue from @code4life's comment in accepted answer, I realized that it is achievable through JArray object as well found in Newtonsoft.Json.Linq namespace. So, I thought of building another answer to provide an alternative in case it helps someone:

    using Newtonsoft.Json.Linq;
    
    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}
        };
    
        return JArray.FromObject(configs).ToString();
    }
    

    Note: Anonymous types which are being created through new { FirstKey = val1, SecondKey = val2, ThirdKey = val3} syntax can very well contain any .Net data type for that matter and not just strings which I've asked in my original post e.g.new { FirstKey = "AnyString", SecondKey = true, ThirdKey = DateTime.Now}

    0 讨论(0)
  • 2021-01-14 17:27

    There's JsonWriter, which is not much more than StringBuilder - you do not get much constraints and can possibly end up with incorrect document

    Some time ago I wanted to create simple documents WITHOUT the overhead of serialization, but with the guarantee they will be correct.

    The pattern I went with is:

    var doc = JSONElement.JSONDocument.newDoc(true);
    
    using (var root = doc.addObject())
    {
        root.addString("firstName", "John");
        root.addString("lastName", "Smith");
        root.addBoolean("isAlive", true);
        root.addNumber("age", 27);
        using (var addres = root.addObject("address"))
        {
            addres.addString("streetAddress", "21 2nd Street");
            addres.addString("city", "New York");
            addres.addString("state", "NY");
            addres.addString("postalCode", "10021-3100");
        }
        using (var phoneNumbers = root.addArray("phoneNumbers"))
        {
            using (var phone = phoneNumbers.addObject())
            {
                phone.addString("type", "home");
                phone.addString("number", "212 555-1234");
            }
    //[....]           
        }        
    }
    string output = doc.getOutput();
    

    }

    The module (< 300lines) is available here: https://github.com/AXImproveLtd/jsonDocCreate

    0 讨论(0)
提交回复
热议问题