2

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..?

7
  • MVC already has a built in method for this. In you controller, initialize a new instance of MassPayList and set its properties then return Json(yourInstance); Commented Jun 9, 2015 at 3:32
  • @StephenMuecke Can you give example?? If thats ok ?? Commented Jun 9, 2015 at 5:15
  • Its simply MassPayList model = new MassPayList(); model.oauth_consumer_key = "some_oauth_token"; and ditto for other properties (including adding new MassPay to property mass_pays) and then in the controller - return Json(model, JsonRequestBehavior.AllowGet);. The Json() method correctly generated the json data for you. Note if its a POST method, then you can omit the second parameter of the Json() method Commented Jun 9, 2015 at 5:23
  • @StephenMuecke So you want me to generate two JSON and combine them ??? Commented Jun 9, 2015 at 5:30
  • I added all properties of mass pay, Hoe can i create multiple instance. i am not sure how many users user will pass.. Its not fixed... So if user passed three array then i have keep count of it.. and how i do that not sure Commented Jun 9, 2015 at 5:32

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

@T McKeown I want to create JSON and the method I am using is not working.. Can U tell me how to write usinh Jobject.??
0

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();
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.