Serialize List> as JSON

后端 未结 2 361
星月不相逢
星月不相逢 2020-12-10 10:38

I\'m very new with JSON, please help!

I am trying to serialise a List> as JSON

Currently:



        
相关标签:
2条回答
  • 2020-12-10 10:56

    So I didn't want to use anything but native c# to solve a similar issue and for reference this was using .net 4, jquery 3.2.1 and backbone 1.2.0.

    My issues was that the List<KeyValuePair<...>> would process out of the controller into a backbone model but when I saved that model the controller could not bind List.

    public class SomeModel {
        List<KeyValuePair<int, String>> SomeList { get; set; }
    }
    
    [HttpGet]
    SomeControllerMethod() {
        SomeModel someModel = new SomeModel();
        someModel.SomeList = GetListSortedAlphabetically();
        return this.Json(someModel, JsonBehavior.AllowGet);
    }
    

    network capture:

    "SomeList":[{"Key":13,"Value":"aaab"},{"Key":248,"Value":"aaac"}]
    

    But even though this set SomeList properly in the backing model.js trying to save the model without any changes to it would cause the binding SomeModel object to have the same length as the parameters in the request body but all the keys and values were null:

    [HttpPut]
    SomeControllerMethod([FromBody] SomeModel){
        SomeModel.SomeList; // Count = 2, all keys and values null.
    }
    

    The only things I could find is that KeyValuePair is a structure and not something that can be instantiated in this manner. What I ended up doing is the following:

    • Add a Model wrapper somewhere that contains key, value fields:

      public class KeyValuePairWrapper {
          public int Key { get; set; }
          public String Value { get; set; }
      
          //default constructor will be required for binding, the Web.MVC binder will invoke this and set the Key and Value accordingly.
          public KeyValuePairWrapper() { }
      
          //a convenience method which allows you to set the values while sorting
          public KeyValuePairWrapper(int key, String value)
          {
              Key = key;
              Value = value;
          }
      }
      
    • Set up your binding class model to accept your custom wrapper object.

      public class SomeModel
      {
          public List<KeyValuePairWrapper> KeyValuePairList{ get; set }; 
      }
      
    • Get some json data out of a controller

      [HttpGet]
      SomeControllerMethod() {
          SomeModel someModel = new SomeModel();
          someModel.KeyValuePairList = GetListSortedAlphabetically();
          return this.Json(someModel, JsonBehavior.AllowGet);
      }
      
    • Do something at a later time, maybe model.save(null, ...) is invoked

      [HttpPut]
      SomeControllerMethod([FromBody] SomeModel){
          SomeModel.KeyValuePairList ; // Count = 2, all keys and values are correct.
      }
      
    0 讨论(0)
  • 2020-12-10 11:05

    You can use Newtonsoft and dictionary:

        var dict = new Dictionary<int, string>();
        dict.Add(1, "one");
        dict.Add(2, "two");
    
        var output = Newtonsoft.Json.JsonConvert.SerializeObject(dict);
    

    The output is :

    {"1":"one","2":"two"}
    

    Edit

    Thanks to @Sergey Berezovskiy for the information.

    You are currently using Newtonsoft, so just change your List<KeyValuePair<object, object>> to Dictionary<object,object> and use the serialize and deserialize method from the package.

    0 讨论(0)
提交回复
热议问题