locked
deserializing JSON array RRS feed

  • Question

  • User-153404742 posted

    Hi,

    I've tried several methods including creating a class object that holds the ids and passing that as a type to deserialize but none works.  How do I deserialize the following JSON array?

    "[{\"p_id\":\"Testing 1\",\"e_id\":1},{\"p_id\":\"This is testing again\",\"e_id\":2}]"

    I want to be able to loop through each item in this array and grab p_id and e_id values.  Your help is appreciated.

    I tried the following and it works partially....where it returns an array but p_id values are null.  the e_id values are showing up fine in the array of strings.

    List<mapping> myDeserializedObjList = (List<mapping>)Newtonsoft.Json.JsonConvert.DeserializeObject(mappingIds, typeof(List<mapping>)); </mapping></mapping></mapping>

    public class Mapping { public string p_id { get; set; } public string e_id { get; set; } }

    Wednesday, May 23, 2018 6:21 PM

All replies

  • User753101303 posted

    Hi,

    Not sure to get how the List type is defined. I would use something such as :

    var dataList= Newtonsoft.Json.JsonConvert.DeserializeObject<List<Mapping>>(value);

    Edit: tried with

    public class List:List<Mapping>{ }

    and it seems your code works as well. Not sure what happens on your side. BTW you get this string from where ? It is not uncommon to see uneeded serializing/deserializing code. If working with http requests, ASP.NET is able to serialize/deserialize data for you and so you can write code that just deals directly with objects.

    Wednesday, May 23, 2018 7:08 PM
  • User-153404742 posted

    I'm actually trying to pass an array from jQuery function into a hiddenfield value.  Directly 

    $('.save-mapping-btn').on('click', function () { var tData = []; var hidden = document.getElementById("hdfMapped"); $('.p-item').each(function (i, el) { var attr = $(el).attr('data-eid'); if (typeof attr !== typeof undefined && attr !== false) { var eid = $(el).data().eid; var pid = $(el).data().id; tData.push({ p_id: pid, e_id: eid, }); } }); $('#hdfMapped').val(JSON.stringify(tData)); // $('#hdfMapped').val(tData.join("|")); I tried the last commented out to pass the array split by "|" but it returns array of just {object}s....JSON returns value in the format I mentioned in my initial post but I can't get it to deserialize correctly. the p_id is returning NULL when I tried to deserialize with my last comment in the initial post.

    Wednesday, May 23, 2018 7:27 PM
  • User753101303 posted

    And so the definition for the List type you are using is ? As I told earlier with :

    public class List:List<Mapping>{ }
    public class Mapping { public string p_id { get; set; } public string e_id { get; set; } }
    

    Both

    List works =(List) Newtonsoft.Json.JsonConvert.DeserializeObject(value,typeof(List));
    var clearer = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Mapping>>(value);

    are working fine. Not sure which definition you are ujsing for List in your code. IMO it's overkill and line 2 is enough. Try what is shown above and see what happens. Mapping is really defined the way you shown ?

    Now posting values as part of an hidden field for a full postback is a bit unusual. I wouldn't have thought about this. Usually I'm posting  form fields or data (as json) but I don't remember to had to mix both.

    Edit: to be on the safe side my test string :

    var value = "[{\"p_id\":\"Testing 1\",\"e_id\":1},{\"p_id\":\"This is testing again\",\"e_id\":2}]";

    Not sure why it doesn't work on your side.

    Wednesday, May 23, 2018 7:44 PM
  • User-153404742 posted

    Type  is  public List<Mapping> Mapping { get; set; }

    Not sure why but p_id returns NULL even with both lines tried above.  I'm going to try to read the string and convert it to a list<list<strings>> as I can't figure out deserializing this object....Thank you for your help.

    Wednesday, May 23, 2018 8:10 PM
  • User753101303 posted

    And you checked your code to see if the Mapping class definition is the really the one you posted ? As it works with one property I would say there is a problem in how the other p_id property is defined ?

    At worst I would create a list that I would then serialize. Do you see p_id in the resulting json string?

    Wednesday, May 23, 2018 9:12 PM