Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to write below JSON in string in C#
{
    "HEADER": {
        "APPLICATION-ID": "82274992",
        "CUST-ID": "82274992"
    },
    "REQUEST": {
        "01": "LOW_PRIORITY",
        "02": "CIR",
        "03": "HOUSING LOAN",
      
        "21": {
            "01": "TEST TEST TEST",
            "02": "TEST"
        },
        "22": "FEMALE",
        "23": "MARRIED",
        "29": [
            {
                "01": "RESIDENCE",
                "02": "Owned"
            }
        ],
        "30": {
            "01": "AAAPA0000D"
        },
        "31": [
            {
                "01": "Mobile Phone",
                "02": "9898989898"
            },
            {
                "01": "Office Phone",
                "02": "303030303"
            }
        ],
        "40": "77",
        "51": "500000"
    }
}


What I have tried:

string Req_json = "";

       StringBuilder sb = new StringBuilder();
       StringWriter sw = new StringWriter(sb);

       using (JsonWriter writer = new JsonTextWriter(sw))
       {
           writer.Formatting = Formatting.Indented;


       }

       return Req_json;
Posted
Updated 28-Dec-17 20:03pm
v4
Comments
Karthik_Mahalingam 29-Dec-17 0:49am    
you mean in string variable ?
Vikram Singh Rathaur 29-Dec-17 1:00am    
yes
Karthik_Mahalingam 29-Dec-17 2:10am    
as hardcoded?
OriginalGriff 29-Dec-17 1:26am    
And?
What does it do that you didn't expect, or not do that you did?

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. So if you don't tell us what problem you are getting, we can't solve it for you...
Use the "Improve question" widget to edit your question and provide better information.

1 solution

Your code doesn't make much sense to me, probably since it is missing a lot of content.

I would suggest looking into newtonsoft json.net library and utilizing json2csharp.com.

The first step would be to copy your JSON and use json2csharp.com to generate a set of classes so you can use it in serializing/deserializng your classes.

Then you just assign your values to the class accordingly. Ex:

C#
var header = new Header();
header.ApplicationId = "82274992";

...etc


Then utilize the json.net library to serialize your class to a a json string.

From their website as an example:

C#
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };

string json = JsonConvert.SerializeObject(product);


This would be an easier solution to convert something to a JSON string rather than trying to write your own JSON builder.
 
Share this answer
 
Comments
Vikram Singh Rathaur 29-Dec-17 2:53am    
then i hot below code , how to use this code pls help

public class HEADER
{
public string APPLICATION_ID { get; set; }
public string CUST_ID { get; set; }
public string REQUEST_TYPE { get; set; }
public string REQUEST_TIME { get; set; }
}

public class __invalid_type__21
{
public string __invalid_name__01 { get; set; }
public string __invalid_name__02 { get; set; }
}

public class __invalid_type__29
{
public string __invalid_name__01 { get; set; }
public string __invalid_name__02 { get; set; }
public string __invalid_name__03 { get; set; }
public string __invalid_name__04 { get; set; }
public string __invalid_name__06 { get; set; }
public string __invalid_name__05 { get; set; }
}

public class __invalid_type__30
{
public string __invalid_name__01 { get; set; }
}

public class __invalid_type__31
{
public string __invalid_name__01 { get; set; }
public string __invalid_name__02 { get; set; }
}

public class REQUEST
{
public string __invalid_name__01 { get; set; }
public string __invalid_name__02 { get; set; }
public string __invalid_name__03 { get; set; }
public string __invalid_name__04 { get; set; }
public string __invalid_name__05 { get; set; }
public string __invalid_name__06 { get; set; }
public string __invalid_name__07 { get; set; }
public string __invalid_name__08 { get; set; }
public string __invalid_name__09 { get; set; }
public string __invalid_name__10 { get; set; }
public string __invalid_name__11 { get; set; }
public string __invalid_name__12 { get; set; }
public string __invalid_name__13 { get; set; }
public string __invalid_name__14 { get; set; }
public string __invalid_name__15 { get; set; }
public string __invalid_name__16 { get; set; }
public string __invalid_name__18 { get; set; }
public string __invalid_name__19 { get; set; }
public string __invalid_name__20 { get; set; }
public __invalid_type__21 __invalid_name__21 { get; set; }
public string __invalid_name__22 { get; set; }
public string __invalid_name__23 { get; set; }
public string __invalid_name__27 { get; set; }
public string __invalid_name__28 { get; set; }
public List<__invalid_type__29> __invalid_name__29 { get; set; }
public __invalid_type__30 __invalid_name__30 { get; set; }
public List<__invalid_type__31> __invalid_name__31 { get; set; }
public string __invalid_name__40 { get; set; }
public string __invalid_name__51 { get; set; }
public string __invalid_name__52 { get; set; }
public string __invalid_name__54 { get; set; }
public string __invalid_name__55 { get; set; }
}

public class RootObject
{
public HEADER HEADER { get; set; }
public REQUEST REQUEST { get; set; }
}
Patrice T 29-Dec-17 4:04am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
David_Wimbley 29-Dec-17 3:02am    
Try using this link since, as you can see, your types in your JSON don't correspond to class/property names correctly.

Either adjust your JSON to map correctly (ex: Application-ID to ApplicationId) or use this link.

https://app.quicktype.io/#r=json2csharp

That link shows you usage and should provide you everything you need to serialize/deserialize.

If you still have trouble I suggest you take a look at newtonsoft json.net website for how to serialize/deserialize your classes to and from JSON strings. It is pretty simple and easy to do, all you need to do is put a little effort into figuring it out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900