XamDev
XamDev

Reputation: 3657

Generate json using Dictionary

I want to generate json in below format.

{
"additionalAttributeBlock": [
    {
        "blockTitle": "B1",
        "B1": [
            {
                "keyNode": "S14",
                "value": "",
                "formula": "",
                "validationID": "",
                "measureID": "5.13",
                "classificationID": "1.1",
                "nodeID": 31,
                "tabCode": "38.1",
                "dataCapID": 0
            }
        ]
    },
    {
        "blockTitle": "B2",
        "B2": [
            {
                "keyNode": "T14",
                "value": "",
                "formula": "",
                "validationID": "",
                "measureID": "5.14",
                "classificationID": "1.1",
                "nodeID": 31,
                "tabCode": "38.1",
                "dataCapID": 0
            }
        ]
    }

]
}

and this is my c# structure and code

public class AdditionalAttribute
{
    public string KeyNode { get; set; }
    public string Value { get; set; }
    public string Formula { get; set; }
    public string ValidationID { get; set; }
    public string MeasureID { get; set; }
    public string ClassificationID { get; set; }
    public int NodeID { get; set; }
    public string TabCode { get; set; }
    public int DataCapID { get; set; }
}

public class AdditionalAttributeBlock
    {
        public AdditionalAttributeBlock()
        {
            AdditionalAttribute = new Dictionary<string, List<AdditionalAttribute>>();
        }
        public string BlockTitle { get; set; }

        public Dictionary<string,List<AdditionalAttribute>> AdditionalAttribute { get; set; }

    }

public class AllEntities
    {
        public List<AdditionalAttributeBlock> AdditionalAttributeBlock { get; set; }
    }

and in code while looping i am adding the list like below .......

additionalAttributeBlock.AdditionalAttribute.Add(itemAddAttrM.SubUnitName, 
lstAdditionalAttributes);

lstAdditionalAttributeBlocks.Add(additionalAttributeBlock);

But it is giving me json in below format with above code

{
    "additionalAttributeBlock": [
        {
            "blockTitle": "% Fe",
"additionalAttribute":{
            "% Fe": [
                {
                    "keyNode": "S14",
                    "value": "",
                    "formula": "",
                    "validationID": "",
                    "measureID": "5.13",
                    "classificationID": "1.1",
                    "nodeID": 31,
                    "tabCode": "38.1",
                    "dataCapID": 0
                },
                {
                    "keyNode": "S15",
                    "value": "",
                    "formula": "",
                    "validationID": "",
                    "measureID": "5.13",
                    "classificationID": "1.2",
                    "nodeID": 31,
                    "tabCode": "38.1",
                    "dataCapID": 0
                }
            ]
}
        },
        {
            "blockTitle": "% Co",
"additionalAttribute":{
            "% Co": [
                {
                    "keyNode": "T14",
                    "value": "",
                    "formula": "",
                    "validationID": "",
                    "measureID": "5.14",
                    "classificationID": "1.1",
                    "nodeID": 31,
                    "tabCode": "38.1",
                    "dataCapID": 0
                },
                {
                    "keyNode": "T15",
                    "value": "",
                    "formula": "",
                    "validationID": "",
                    "measureID": "5.14",
                    "classificationID": "1.2",
                    "nodeID": 31,
                    "tabCode": "38.1",
                    "dataCapID": 0
                }
            ]
            }
        }




    ]
}

SO how can i generate the correct json? I have tried with different variations and combinations but it doesn't given me the result which I am looking for.

Do I need to change the structure of c# classes/models to generate the json in correct format OR the rendering way I need to change.

Upvotes: 0

Views: 218

Answers (2)

vasily.sib
vasily.sib

Reputation: 4002

As I can say, what are you trying to achive is this JSON (pseudo-json-code):

{
    "additionalAttributeBlock": [
        {
            "blockTitle": <SOME_BLOCK_TITLE>,
            <SOME_BLOCK_TITLE>: [
                { AdditionalAttribute_INSTANCE_1 },
                { AdditionalAttribute_INSTANCE_2 }
            ]
        },
        {
            "blockTitle": <ANOTHER_BLOCK_TITLE>,
            <ANOTHER_BLOCK_TITLE>: [
                { AdditionalAttribute_INSTANCE_3 }
            ]
        }
    ]
}

Well, this is prety easy to implement for serialization. Just inherit your AdditionalAttributeBlock class from Dictionary:

public class AdditionalAttributeBlock : Dictionary<string, object> { }

That's it! Now, to reproduce sample JSON from your question:

var b1 = new AdditionalAttributeBlock();
b1["BlockTitle"] = "B1";
b1["B1"] = new AdditionalAttribute { ... };

var b2 = new AdditionalAttributeBlock();
b2["BlockTitle"] = "B2";
b2["B2"] = new AdditionalAttribute { ... };

var allBlocks = new List<AdditionalAttributeBlock> { b1, b2 };
var allEntities = new AllEntities { AdditionalAttributeBlock = allBlocks };
var json = JsonConvert.SerializeObject(allEntities);

Now json will contain exactly same result as from your question.

Also, you may add constructor and some getters to AdditionalAttributeBlock class to simplify your work:

public class AdditionalAttributeBlock : Dictionary<string, object>
{
    public string BlockTitle => this["BlockTitle"] as string;
    public AdditionalAttribute Attribute => this[BlockTitle] as AdditionalAttribute;

    public AdditionalAttributeBlock(string title, AdditionalAttributeBlock attribute)
    {
        this["BlockTitle"] = title;
        this[title] = attribute;
    }
}

NOTE: Side-effect of this solution is that now it is not so easy to Deserialize object from JSON string:\ but, as your question is "Generate json using Dictionary", I will leave it on you and strongly suggest to create a custom JsonConverter for this (here is a sample).

Upvotes: 2

Hossein
Hossein

Reputation: 3113

First of all, You must get class model of json from json2csharp .

public class B1
{
    public string keyNode { get; set; }
    public string value { get; set; }
    public string formula { get; set; }
    public string validationID { get; set; }
    public string measureID { get; set; }
    public string classificationID { get; set; }
    public int nodeID { get; set; }
    public string tabCode { get; set; }
    public int dataCapID { get; set; }
}

public class B2
{
    public string keyNode { get; set; }
    public string value { get; set; }
    public string formula { get; set; }
    public string validationID { get; set; }
    public string measureID { get; set; }
    public string classificationID { get; set; }
    public int nodeID { get; set; }
    public string tabCode { get; set; }
    public int dataCapID { get; set; }
}

public class AdditionalAttributeBlock
{
    public string blockTitle { get; set; }
    public List<B1> B1 { get; set; }
    public List<B2> B2 { get; set; }
}

public class RootObject
{
    public List<AdditionalAttributeBlock> additionalAttributeBlock { get; set; }
}

Then you can do it with Newtonsoft.Json like this:

var data= Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json);

// You can add into B1/B2
data.additionalAttributeBlock.Add(...);

Other way You can do it with JSON.Net without json model.

JObject data = JObject.Parse(jsonText);
JObject additionalAttributeBlock= data["additionalAttributeBlock"] as JObject;
//additionalAttributeBlock.Add("name", JObject.Parse(yourjsonText);
additionalAttributeBlock.Add("name",JObject.FromObject(obj);

Upvotes: 0

Related Questions