Skip to content
Loading
Deserialize JSON Question
  • So I got what I needed working mostly. I was able to extract the information I wanted, but ran into another issue. 
     
    So I have my json object with what I want to pull. The issue I have now is it iterates through the object assigning all the vaules and does not stop until the end. I cannot get it to break either. so say I have 
     
    public class JsonObject
    {
     
    [JsonProperty(PropertyName = "manuf")]
    public string deviceManuf { get; set; }
     
    [JsonProperty(PropertyName = "key")]
    public string deviceKey { get; set; }
     
    [JsonProperty(PropertyName = "phyname")]
    public string devicePhyName { get; set; }
      
    } 
     
    Then the class to grab the data 
    private static string GetJsonData()
    {
          about 12 lines of code to get the data then finally 
          var objInfo = JsonConvert.DeserializeObject>(content); 
    } 
    Then inside the main class we have 
    static void Main(string[] args)
    {
          GetKismetString();
    }
     
    The Json data I am pulling in is a big list of devices so after it sets all the fields for the first device instead of letting me stop and move the data or do something with it it just blows through all of the file and I am left with the object only containing the last device. I tried to break out and that did not help. Ultimatly what I want to do is write the obect information into a sql data base. Any advise would be greatly apreciated. THank you.  
    +1
  • Thank you all for your replies. I believe I now have the information I need to finish what I am doing. Most appreciated and all of the replies were very helpful.
    0
  • Laxmidhar Sahoo
    hey scott
    here prop.name is the column name .So you can filter in foreach loop with some contidtion  in if cluse
    0
  • Veerendra Annigere
    you can use jsonpath and take any data from your json.
    Check this.
     
    https://jsonpath.com/
    0
  • Back to my question. Is it required to parse all fields in the json file or can I pick and choose? Below is a link to the information I am trying to sort through. Its a json file produced by kismet to report on. I only need a few fields of data like SSID, Channel, Device Name, First and Last seen date. I am hoping I do not need to parse the entire file. It is very large. 
     
    https://drive.google.com/open?id=1nIYSAcBy6OxbtJzi230_kAAIjer3h9W3 
    +1
  • Jignesh Kumar
    Hi Scott,
    Create class for number of columns you want to deserialize and try below code to do mapping for specific column,
    1. public class ClassDeserializeExample  
    2.    {  
    3.        public string field1 { get; set; }  
    4.        public string field2 { get; set; }  
    5.        public string field3 { get; set; }  
    6.        public string field4 { get; set; }  
    7.   
    8.        private void DesirializeJson()  
    9.        {  
    10.            dynamic lresultData = JsonConvert.DeserializeObject(jsonContent);  
    11.   
    12.            ClassDeserializeExample locationToReturn = new ClassDeserializeExample()  
    13.            {  
    14.                field1 = lresultData.result.field1,  
    15.                field2 = lresultData.result.field2,  
    16.                field3 = lresultData.result.field3,  
    17.                field4 = lresultData.result.field4  
    18.            };  
    19.   
    20.            Console.WriteLine("field1: " + lresultData.field1);  
    21.            Console.WriteLine("field2: " + lresultData.field2);  
    22.   
    23.            Console.ReadLine();  
    24.        }  
    25.    } 
     
     
    +1