Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

c# - JSON.net Generating a lot of extra Square brackets

I am trying to generate the following json with JSON.Net but it is adding alot of extra square brackets with what should I do
Needed Json

 {
    "addUser": {
      "idCard": "xxx",
      "firstName": "xxx",
      "surname": "Muscat",
      "isActive": true,
      "titleDesc": "xx",
      "genderDesc": "Female",
      "emailAddress":"",
      "mobileNumber":"",
      "telephoneNumber":"",
      "dob":""
    }
  }

C# code

var obj = new JObject();
obj.Add(
    new JProperty("addUser",
        new JArray(
            new JObject(
                new JProperty("idCard", doct.First().idCard.PadLeft(8, '0')),
                new JProperty ("firstName", det.First().PersonName),
                new JProperty("surname", det.First().PersonSurname),
                new JProperty("isActive", true),
                new JProperty("titleDesc", ""),
                new JProperty("genderDesc", det.First().PersonGenderDesc),
                new JProperty("emailAddress", ""),
                new JProperty("mobileNumber", ""),
                new JProperty("telephoneNumber", ""),
                new JProperty("dob", det.First().PersonBirthDate)
                ))));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you already have a json string and want it to map it to a C# class construct you can use the intigrated Visual Studio function Paste Json as Classes.

  1. Copy some JSON
  2. Select Edit –> Paste Special –> Paste JSON As Classes

If you do so Visual Studio will make for you this two classes:

public class Rootobject
{
   public Adduser addUser { get; set; }
}

public class Adduser
{
   public string idCard { get; set; }
   public string firstName { get; set; }
   public string surname { get; set; }
   public bool isActive { get; set; }
   public string titleDesc { get; set; }
   public string genderDesc { get; set; }
   public string emailAddress { get; set; }
   public string mobileNumber { get; set; }
   public string telephoneNumber { get; set; }
   public string dob { get; set; }
}

With this Visual Studio tool you get a first idea how to map your json string to C# classes.

Now if you have an object of your class, you can simply convert it with JsonConvert

var myObject = new Rootobject() { addUser = new Adduser() { idCard = 1, ...} };
var json = JsonConvert.SerializeObject(myObject);

To deserialize you simply call:

var myObject = JsonConvert.DeserializeObject<Rootobject>(json); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...