-1

I am sending URL request and getting response in curl and then convert into a json...object inside object(contain numeric and dot(924.136028459)) like:

string arr1 = 
    "{  
       "success":1,
       "results":[  
          {  
             "Markets":{  
                "924.136028459":{  
                   "productType":"BOOK1",
                   "key":"SB_MARKET:924.136028459"
                },
                "924.136028500":{  
                   "productType":"BOOK2",
                   "key":"SB_MARKET:924.136028459"
                }
             }
          }
       ]
    }";

I have created properties class ..but i am not understanding how can we access inside "924.136028500" attributes

public class Json
{
    public System.Collections.ObjectModel.Collection<Arr> results { get; set; }
}

public class Arr
{
    public sp Markets { get; set; }
}

public class sp
{
    public string productType { get; set; }
    public string key { get; set; }
}

and I am using deserialize code...

var serializer = new JavaScriptSerializer();
Json json = serializer.Deserialize<Json>(arr1);
Gert Kommer
  • 1,163
  • 2
  • 22
  • 49
  • Can you be a bit more clear? It is not really clear what your question is and what is going wrong. – Gert Kommer May 15 '18 at 15:31
  • how can i make properties class to access this json string? – P.Chauhan May 15 '18 at 15:38
  • Make `Markets` be a `public Dictionary Markets { get; set; }` as shown in [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/a/24536564/3744182) or [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/a/34213724/3744182). – dbc May 15 '18 at 16:59
  • 1
    Also, while `Dictionary` will work with `JavaScriptSerializer`, you should consider switching to [tag:json.net] since `JavaScriptSerializer` is deprecated, according to its [docs](https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx). – dbc May 15 '18 at 17:02

1 Answers1

0

With Cinchoo ETL - an open source library, you can load the Market object easily with few lines of code

string json = @"{  
       ""success"":1,
       ""results"":[
          {  
             ""Markets"":{  
                ""924.136028459"":{  
                   ""productType"":""BOOK1"",
                   ""key"":""SB_MARKET:924.136028459""

                },
                ""924.136028500"":{  
                   ""productType"":""BOOK2"",
                   ""key"":""SB_MARKET:924.136028459""
                }
             }
          }
       ]
    }
";

foreach (var rec in ChoJSONReader<sp>.LoadText(json).WithJSONPath("$..Markets.*"))
    Console.WriteLine($"ProductType: {rec.productType}, Key: {rec.key}");

Output:

ProductType: BOOK1, Key: SB_MARKET:924.136028459
ProductType: BOOK2, Key: SB_MARKET:924.136028459

Checkout CodeProject article for some additional help.

Disclaimer: I'm the author of this library.

Cinchoo
  • 6,088
  • 2
  • 19
  • 34