无法正确格式化为Json

时间:2018-12-31 09:04:24

标签: c# json

我受命调用一个API,该API需要以JSON作为请求。Json应该像这样:

{"customer_number": "Cnum",
  "route_stops":[
         {"company": "My Company",
          "contact": { 
                "name": "Fname Lname",
                "phone": "0000000000"}}
                ]}

我能得到的最接近的是这个

{"customer_number": "Cnum",
 "route_stops": [
        {"company": "My Company",
         "contact": [{
               "name": "Fname Lname",
               "phone": "0000000000"}]}
                ]}

我所做的是下面的代码:

    public class Order
    {
        public string customer_number;        
        public object[] route_stops = new object[2];
    }
    public class Contact
    {
        public string name;
        public string phone;
    }
    public class PickupStop
    {
        public string company;
        public object[] contact = new object[1];
    }

    static void Main(string[] args)
    {
        Contact ctPickupStops = new Contact();
        ctPickupStops.name = "Fname Lname";
        ctPickupStops.phone = "0000000000";

        PickupStop ps = new PickupStop();
        ps.company = "My Company";          

        ps.contact[0] = ctPickupStops;

        Order ord = new Order();
        ord.customer_number = "Cnum";         
        ord.route_stops[0] = ps;           

        string output = JsonConvert.SerializeObject(ord);                                                         
    }

有什么想法可以从联系人中删除“ []”括号吗?

3 个答案:

答案 0 :(得分:2)

我建议您使用以下内容:-

您可以使用此工具JSONLINT来验证Json格式

您也可以将json2csharpjson转换为c#

这将是您的json格式:-

{
    "customer_number": "Cnum",
    "route_stops": [{
        "company": "My Company",
        "contact": {
            "name": "Fname Lname",
            "phone": "0000000000"
        }
    }]
}

一旦将json转换为c#,它将是:-

public class Contact
{
    public string name { get; set; }
    public string phone { get; set; }
}

public class RouteStop
{
    public string company { get; set; }
    public Contact contact { get; set; }
}

public class RootObject
{
    public string customer_number { get; set; }
    public List<RouteStop> route_stops { get; set; }
}

答案 1 :(得分:0)

这对我有用:

class Program
    {
        static void Main()
        {
            var root = new RootObject() { customer_number = "Cnum", route_stops = new List<RouteStop>() };
            var routeStops = new List<RouteStop>
            {
                new RouteStop
                {
                    company = "My Company",
                    contact = new Contact{ name = "Fname Lname", phone="0000000000" }
                }
            };

            root.route_stops = routeStops;

            string output = JsonConvert.SerializeObject(root);

            Console.ReadKey();
        }
    }

    public class Contact
    {
        public string name { get; set; }
        public string phone { get; set; }
    }

    public class RouteStop
    {
        public string company { get; set; }
        public Contact contact { get; set; }
    }

    public class RootObject
    {
        public string customer_number { get; set; }
        public List<RouteStop> route_stops { get; set; }
    }

答案 2 :(得分:0)

Please follow the below:
{"customer_number": "Cnum",
  "route_stops":[
         {"company": "My Company",
          "contact": { 
                "name": "Fname Lname",
                "phone": "0000000000"}}
                ]}

 public class Order
    {
        public string customer_number;        
        public List<route_stops> route_stops;
    }

 public class route_stops
    {
        public string company;        
        public List<Contact> contact;

    }

public class Contact
    {
         public string name;
        public string phone;

    }