-1
{
  "authType": "BASIC",
  "ipAddress": "192.168.210.152",
  "methodLog": [
    {
      "count": 12,
      "methodName": "getPrefixLicensee",
      "lastAccessTimestamp": 1475574521425,
      "firstAccessTimestamp": 1475563584777
    }
  ],
  "requesterGLN": "9501101020016",
  "onBehalfOfGLN": "9501101020016",
  "totalAccessCount": 12,
  "accessCountPer24Hrs": 12,
  "lastAccessTimestamp": 1475574521425,
  "firstAccessTimestamp": 1475563584777,
  "firstAccessWithin24hrsTimestamp": 1475563584777
}

How do i get the values in methodLog using Newtonsoft JSON?

Thanks, John

Jite
  • 5,761
  • 2
  • 23
  • 37
  • 1
    Show what you have tried. Have you created any type of Object from the JSON? – Jite Dec 09 '16 at 08:10
  • 4
    Deserialize it to an object and then you can access the objects array? – Carra Dec 09 '16 at 08:14
  • 2
    There are thousands of similar questions. Just create the required classes, using http://json2csharp.com/ and then JsonConvert.DeserializeObject(json) – Mahdi Dec 09 '16 at 08:18
  • Possible duplicate of [Newtonsoft JSON Deserialize](http://stackoverflow.com/questions/17038810/newtonsoft-json-deserialize) – Nameless One Dec 09 '16 at 08:24

2 Answers2

0

You can use it by creating class for child data and use that child data class as datatype in parent class and then you can use it..

like...

public class MethodLog
{
    public int count { get; set; }
    public string methodName { get; set; }
    public string lastAccessTimestamp { get; set; }
    public bool firstAccessTimestamp { get; set; }
}

public class Data
{
    public string authType { get; set; }
    public string ipAddress { get; set; }
    public List<MethodLog> MethodLog{ get; set; }
    public string requesterGLN { get; set; }
    public string onBehalfOfGLN { get; set; }
    public int totalAccessCount { get; set; }
    public int accessCountPer24Hrs { get; set; }
    public string lastAccessTimestamp { get; set; }
    public string firstAccessTimestamp { get; set; }
    public string firstAccessWithin24hrsTimestamp { get; set; }
}

public class SingleData
{
    public Data data { get; set; }
}

NOTE:- You can change datatype as per your need

Priyank_Vadi
  • 1,028
  • 10
  • 27
0

Just create classes for your objects and use DeserializeObject<T>:

void Main()
{
    var json = File.ReadAllText(@"C:\pathtojson.json");
    var result = JsonConvert.DeserializeObject<RootObject>(json);
    //Generic Linqpad output:
    result.methodLog.Dump();

}

public class MethodLog
{
    public int count { get; set; }
    public string methodName { get; set; }
    public long lastAccessTimestamp { get; set; }
    public long firstAccessTimestamp { get; set; }
}

public class RootObject
{
    public string authType { get; set; }
    public string ipAddress { get; set; }
    public List<MethodLog> methodLog { get; set; }
    public string requesterGLN { get; set; }
    public string onBehalfOfGLN { get; set; }
    public int totalAccessCount { get; set; }
    public int accessCountPer24Hrs { get; set; }
    public long lastAccessTimestamp { get; set; }
    public long firstAccessTimestamp { get; set; }
    public long firstAccessWithin24hrsTimestamp { get; set; }
}

Result:

enter image description here

Marco
  • 22,856
  • 9
  • 75
  • 124