nikunjM
nikunjM

Reputation: 590

How to Create multi level Json using Jobject in C#?

I want to create multi level Json, Using http://json2csharp.com/. I created classes. But not sure how to use it.

public class MassPay
{
    public string legal_name { get; set; }
    public string account_number { get; set; }
    public string routing_number { get; set; }
    public string amount { get; set; }
    public string trans_type { get; set; }
    public string account_class { get; set; }
    public string account_type { get; set; }
    public string status_url { get; set; }
    public string supp_id { get; set; }
    public string user_info { get; set; }
}

public class MassPayList
{
    public string oauth_consumer_key { get; set; }
    public string bank_id { get; set; }
    public string facilitator_fee { get; set; }
    public IList<MassPay> mass_pays { get; set; }

}

These are my classes and this is Json Format i want to create...

there are extra elements...

{
"oauth_consumer_key":"some_oauth_token", 
    "mass_pays":[
    {"legal_name":"SomePerson1",
    "account_number":"888888888",
    "routing_number":"222222222",
    "amount":"10.33",
    "trans_type":"0",
    "account_class":"1",
    "account_type":"2"
    },
    {"legal_name":"SomePerson2",
    "account_number":"888888888",
    "routing_number":"222222222",
    "amount":"10.33",
    "trans_type":"0",
    "account_class":"1",
    "account_type":"1"}
    ]
    }

So far i have come up with below code..I am using JObject, and all others wer single level so it was pretty easy. but when it comes to two or three level its difficult.

public JObject AddMassPayRequest(MassPayList lMassPayList, MassPay lMassPay)
        {
            JObject pin = new JObject(
                new JProperty("legal_name", lMassPay.legal_name),
                new JProperty("account_number", lMassPay.account_number),
                new JProperty("routing_number", lMassPay.routing_number),
                new JProperty("amount", lMassPay.amount),
                new JProperty("trans_type", lMassPay.trans_type),
                new JProperty("account_class", lMassPay.account_class),
                new JProperty("account_type", lMassPay.account_type),
                new JProperty("status_url", lMassPay.status_url),
                new JProperty("supp_id", lMassPay.supp_id),
                new JProperty("status_url", lMassPay.status_url),
                new JProperty("user_info", lMassPay.user_info)
           );
            return pin;
        }
        public JObject AddMassPayRequestList(MassPayList lMassPayList, MassPay lMassPay)
        {
            JObject pin = new JObject(
                new JProperty("mass_pays", lMassPayList.mass_pays),
                new JProperty("bank_id", lMassPayList.bank_id),
                new JProperty("facilitator_fee", lMassPayList.facilitator_fee),
                new JProperty("oauth_consumer_key", lMassPayList.oauth_consumer_key)
          );
            return pin;
        }

Can some one help me how to do this..?

Upvotes: 2

Views: 1146

Answers (2)

nikunjM
nikunjM

Reputation: 590

So finally I got this answer, Its simple structure. Using this u can create any type of Json... It doesnt have to follow same structure..

The logic behind this is add things you want at start, create class and inside that properties you want to add into json. SO while passign just add for loop and pass Object to the list.. It will loop through and create JSon for You..

If you have any doubts, let me know happy to help you

 public String ToJSONRepresentation(List<MassPay> lMassPay)
        {

            StringBuilder sb = new StringBuilder();
            JsonWriter jw = new JsonTextWriter(new StringWriter(sb));

            jw.Formatting = Formatting.Indented;
            jw.WriteStartObject();
            jw.WritePropertyName("oauth_consumer_key");
            jw.WriteValue("asdasdsadasdas");
            jw.WritePropertyName("mass_pays");
            jw.WriteStartArray();

            int i;
            i = 0;

            for (i = 0; i < lMassPay.Count; i++)
            {
                jw.WriteStartObject();
                jw.WritePropertyName("legal_name");
                jw.WriteValue(lMassPay[i].legal_name);
                jw.WritePropertyName("account_number");
                jw.WriteValue(lMassPay[i].account_number);
                jw.WritePropertyName("routing_number");
                jw.WriteValue(lMassPay[i].routing_number);
                jw.WritePropertyName("amount");
                jw.WriteValue(lMassPay[i].amount);
                jw.WritePropertyName("trans_type");
                jw.WriteValue(lMassPay[i].trans_type);
                jw.WritePropertyName("account_class");
                jw.WriteValue(lMassPay[i].account_class);
                jw.WritePropertyName("account_type");
                jw.WriteValue(lMassPay[i].account_type);
                jw.WritePropertyName("status_url");
                jw.WriteValue(lMassPay[i].status_url);
                jw.WritePropertyName("supp_id");
                jw.WriteValue(lMassPay[i].supp_id);
                jw.WriteEndObject();
            }
            jw.WriteEndArray();
            jw.WriteEndObject();
            return sb.ToString();
        }

Upvotes: 0

T McKeown
T McKeown

Reputation: 12857

if you're using ASP.NET MVC you just need to use the Json response action using your existing classes.

You could simply do something like this in a controller:

return Json(new { PoId = newPoId, Success = true });

or an actual concrete model class:

var _AddMassPayRequestList = new AddMassPayRequestList();
  ...

returning a populated instance of your AddMassPayRequestList class:

return Json(_AddMassPayRequestList);

Upvotes: 1

Related Questions