1

Dear All I am using C# Class and make the json object but how it call it. and show the json object I am show the code please help me.

Here I am create a class

public class Contacts
{
    public List<PhoneMobile> phoneMobiles { get; set; }
    public List<PhoneLandline> phoneLandlines { get; set; }
    public List<Email> emails { get; set; }
}
public class PhoneMobile
{
    public string phoneMobile { get; set; }
}

Here i am use the class like this

contacts = new Contacts
    {
        phoneMobiles = new List<PhoneMobile>
        {

        },
        phoneLandlines = new List<PhoneLandline>(),
        emails = new List<Email>(),
    }

I want SerializeObject Like This objects give how to put a value and make it.

"contacts": {
      "phoneMobiles": [
        {
          "phoneMobile": "8103267511"
        }
      ],
      "phoneLandlines": [
        {
          "phoneLandLineNumber": "8103267511"
        }
      ],
      "emails": [
        {
          "email": "[email protected]"
        }
      ]
    },
    "contactPerson": [
      {
        "personName": "TEST KARKHANA",
        "owner": "null",
        "email": "[email protected]",
        "phone": "8602865989"
      }
    ], 

How To Make It Please Help

9
  • Welcome to stackoverflow. I'm sorry but your question is not clear... where do you want to "show it"? Commented Sep 28, 2018 at 9:20
  • I want make a json object like this using c# classes "contacts": { "phoneMobiles": [ { "phoneMobile": "8103267511" } ], "phoneLandlines": [ { "phoneLandLineNumber": "8103267511" } ], "emails": [ { "email": "[email protected]" } ] }, "contactPerson": [ { "personName": "TEST KARKHANA", "owner": "null", "email": "[email protected]", "phone": "8602865989" } ], Commented Sep 28, 2018 at 9:21
  • What is contactPerson class? Commented Sep 28, 2018 at 9:22
  • 1
    link use this for creating model in c# based on your json, it's helpfull and saves time !! @RomanMarusyk got your answer allready Commented Sep 28, 2018 at 9:32
  • 1
    @DavidG - I've now had a chance to play with Newtonsoft JSON.Net. I have one complaint... why didn't you tell me about it earlier?! ;-) Commented Oct 17, 2018 at 16:52

1 Answer 1

3

Use Json.Net library, you can download it from Nuget. Try this

var contactCollection = new Contacts
{
    phoneMobiles = new List<PhoneMobile>
    {
        new PhoneMobile { phoneMobile = "8103267511" }
    },
    phoneLandlines = new List<PhoneLandline>()
    {
        new PhoneLandline { phoneLandLineNumber = "8103267511" }
    },
    emails = new List<Email>()
    {
        new Email { email = "[email protected]" }
    }
};

var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(contacts);

It will serialize object to json except contactPerson section

If you need serialize object with root name contacts then try

var collectionWrapper = new {
    contacts = contactCollection
};

var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(collectionWrapper);

then the result will be like:

{"contacts":{"phoneMobiles":[{"phoneMobile":"8103267511"}], "phoneLandlines":[{"phoneLandLineNumber":"8103267511"}], "emails":[{"email":"[email protected]"}]}}
Sign up to request clarification or add additional context in comments.

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.