0
{
  "Sts": 1,
  "TMtd": 2,
  "SId": 215,
  "T": 1599453168,
  "CCSr": 98972,
  "TId": 492,
  "UId": 1687,
  "NPro": 3,
  "P1": {
    "SKUId": "006920180209601",
    "Q": 1
  },
  "P2": {
    "SKUId": "006954767430522",
    "Q": 1
  },
  "P3": {
    "SKUId": "006954767410623",
    "Q": 1
  }
}

in this P1,P2,P3 is not fixed in other word we can say that in one request it is only P1 but next request it should be P1,P2,P3...... Please suggest me what should i do. I tried it by class but P1,P2,P3 is not fixed that's why stuck Thanks in advance!

aepot
  • 4,558
  • 2
  • 12
  • 24
  • What do you mean they're not fixed? Their properties change? Are there some times more properties? – avenmia Sep 10 '20 at 15:38
  • The Json should have defined that as an array or something. If it's not something you can edit you could probably write a custom deserializer. https://stackoverflow.com/questions/40439290/custom-deserialization-using-json-net might be a good starting point. – asawyer Sep 10 '20 at 15:39
  • Schema of P1, P2 and P3 are fixed or variable(unstructured) – Amardeep Kumar Agrawal Sep 10 '20 at 15:39
  • yes correct it depends on NPro properties if NPro = 1 then only P1 if NPro = 2 then it will be P1,P2 and if Npro = n then it will be P1,P2,P3,P4,....Pn – Shailendra Pal Sep 10 '20 at 15:41
  • @AmardeepKumarAgrawal Scema of P1,P2,P3 will be same – Shailendra Pal Sep 10 '20 at 15:42
  • All of the properties of P1...PN are the same though? – avenmia Sep 10 '20 at 15:45
  • https://stackoverflow.com/a/55569820/7331107 Does this answer help? – avenmia Sep 10 '20 at 15:47
  • If the properties of P1...PN are the same, then can you change your JSON to use an array, e.g., `{"Sts": 1, "PS": [ { "SKUId": "006954767410623", "Q": 2 }, { "SKUId": "006954767410623", "Q": 2 }, { "SKUId": "006954767410623", "Q": 2 } ]}`? Then you can have as many Ps as needed. –  Sep 10 '20 at 15:49
  • @avenmia no its not help because in my json properties are not fixed that's why i unable to understand how can i serialize? – Shailendra Pal Sep 10 '20 at 15:55
  • 1
    This is the result of a type you are serializing? Or is it something your consuming and need to deserialize? Both? – asawyer Sep 10 '20 at 15:58
  • actually this is json which will recieve and need to deserialize this and perform other task – Shailendra Pal Sep 10 '20 at 16:00
  • 1
    What kind of serializer are you using? Please show your best try. – aepot Sep 10 '20 at 16:32

4 Answers4

3

This was written in Linqpad (hence the .Dump()) with the Newtonsoft Json library. Per my comment earlier, this uses a custom deserialization converter. This is pretty quick and probably not optimal and has no error checking code so use at your discretion.

void Main()
{
    var input0p = "{ \"Sts\": 1, \"TMtd\": 2, \"SId\": 215, \"T\": 1599453168, \"CCSr\": 98972, \"TId\": 492, \"UId\": 1687, \"NPro\": 3 }";
    var input1p = "{ \"Sts\": 1, \"TMtd\": 2, \"SId\": 215, \"T\": 1599453168, \"CCSr\": 98972, \"TId\": 492, \"UId\": 1687, \"NPro\": 3, \"P1\": { \"SKUId\": \"006920180209601\", \"Q\": 1 } }";
    var input2p = "{ \"Sts\": 1, \"TMtd\": 2, \"SId\": 215, \"T\": 1599453168, \"CCSr\": 98972, \"TId\": 492, \"UId\": 1687, \"NPro\": 3, \"P1\": { \"SKUId\": \"006920180209601\", \"Q\": 1 }, \"P2\": { \"SKUId\": \"006954767430522\", \"Q\": 1 } }";
    var input3p = "{ \"Sts\": 1, \"TMtd\": 2, \"SId\": 215, \"T\": 1599453168, \"CCSr\": 98972, \"TId\": 492, \"UId\": 1687, \"NPro\": 3, \"P1\": { \"SKUId\": \"006920180209601\", \"Q\": 1 }, \"P2\": { \"SKUId\": \"006954767430522\", \"Q\": 1 }, \"P3\": { \"SKUId\": \"006954767410623\", \"Q\": 1 } }";
    var input4p = "{ \"Sts\": 1, \"TMtd\": 2, \"SId\": 215, \"T\": 1599453168, \"CCSr\": 98972, \"TId\": 492, \"UId\": 1687, \"NPro\": 3, \"P1\": { \"SKUId\": \"006920180209601\", \"Q\": 1 }, \"P2\": { \"SKUId\": \"006954767430522\", \"Q\": 1 }, \"P3\": { \"SKUId\": \"006954767410623\", \"Q\": 1 }, \"P4\": { \"SKUId\": \"006954767414444\", \"Q\": 4 } }";
    var input5p = "{ \"Sts\": 1, \"TMtd\": 2, \"SId\": 215, \"T\": 1599453168, \"CCSr\": 98972, \"TId\": 492, \"UId\": 1687, \"NPro\": 3, \"P1\": { \"SKUId\": \"006920180209601\", \"Q\": 1 }, \"P2\": { \"SKUId\": \"006954767430522\", \"Q\": 1 }, \"P3\": { \"SKUId\": \"006954767410623\", \"Q\": 1 }, \"P4\": { \"SKUId\": \"006954767414444\", \"Q\": 4 }, \"P5\": { \"SKUId\": \"006954767455555\", \"Q\": 5 } }";
    
    Test(input0p);
    Test(input1p);
    Test(input2p);
    Test(input3p);
    Test(input4p);
    Test(input5p);
}

public void Test(string input)
{
    var result = JsonConvert.DeserializeObject<Data>(input);
    result.Dump();
}

[JsonConverter(typeof(DataConverter))]
public class Data
{
    public int Sts { get; set; }
    public int TMtd { get; set; }
    public int SId { get; set; }
    public int T { get; set; }
    public int CCSr { get; set; }
    public int TId { get; set; }
    public int UId { get; set; }
    public int NPro { get; set; }
    public ICollection<Element> PValues { get; set;}
}

public class Element
{
    public string SKUId { get; set; }
    public int Q { get; set; }
}

public class DataConverter : JsonConverter<Data>
{
    public override Data ReadJson(JsonReader reader, Type objectType, [AllowNull] Data existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        var jobj = JObject.Load(reader);
        var result = new Data()
        {
            Sts = jobj["Sts"].Value<int>(),
            TMtd = jobj["TMtd"].Value<int>(),
            SId = jobj["SId"].Value<int>(),
            T = jobj["T"].Value<int>(),
            CCSr = jobj["CCSr"].Value<int>(),
            TId = jobj["TId"].Value<int>(),
            UId = jobj["UId"].Value<int>(),
            NPro = jobj["NPro"].Value<int>(),
            PValues = new List<Element>()
        };
        for (int i = 1; jobj.ContainsKey($"P{i}"); i++)
        {
            var p = jobj[$"P{i}"];
            result.PValues.Add(new Element()
            {
                SKUId = p["SKUId"].Value<string>(),
                Q = p["Q"].Value<int>()
            });
        }
        return result;
    }

    public override void WriteJson(JsonWriter writer, [AllowNull] Data value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

Output:

Example output

asawyer
  • 17,642
  • 8
  • 59
  • 87
0

You can use ExpandoObject like this: Convert from JSON object to expando object in c#

Note: If you are using Unity, IL2CPP does NOT support c# dynamic

MrFlorius
  • 13
  • 1
  • 2
0

You could also deserialize the JSON into a class that has: strings ssts - Npro, then 3 nested instances of class P1-P3. Make sure that the nested class has a constructor that sets its values,

public class PClass
{
   public string SKUId {get;set;}
   public int Q {get;set;}

   public PClass()
   {
      SKUId = "";
      Q = 0;
   }
}

public class MainObject
{
   public string Sts {get;set;} 
   ...
   public int NPro {get;set;}
   public PClass P1 {get;set;}
   public PClass P2 {get;set;}
   public PClass P3 {get;set;}

   public MainClass()
   {
      P1 = new PClass();
      P2 = new PClass();
      P3 = new PClass();
   }
 
}

Something to this effect should allow those P objects to be filled in by the deserializer, or initialized to be a default "" and 0 when they don't exist. Then you can just process the P values depending on the NPro value as appropriate.

Carson
  • 864
  • 1
  • 6
  • 19
0

Create a model with your input data using paste special in visual studio as Edit->Paste Special->Paste Json As Classes. I have created one for you.

public class JsonModel
{
    public int Sts { get; set; }
    public int TMtd { get; set; }
    public int SId { get; set; }
    public int T { get; set; }
    public int CCSr { get; set; }
    public int TId { get; set; }
    public int UId { get; set; }
    public int NPro { get; set; }
    public P1 P1 { get; set; }
    public P2 P2 { get; set; }
    public P3 P3 { get; set; }
}

public class P1
{
    public string SKUId { get; set; }
    public int Q { get; set; }
}

public class P2
{
    public string SKUId { get; set; }
    public int Q { get; set; }
}

public class P3
{
    public string SKUId { get; set; }
    public int Q { get; set; }
}

Now Install nuget package Newtonsoft.Json and reference same in your class. use below code to deserialize the same

JsonModel data = JsonConvert.DeserializeObject<JsonModel>(jsonModel);

I have tried this with two inputs

  1. When p1, p2 and p3 was present
  2. When just p1 and p3 was present

Hope this works for you !