0

This is my json string in database table

{
    "Object":"order",
    "LiveMode":true,
    "Amount":3000,
    "Captured":false,
    "Created":"\/Date(1593359683000)\/",
    "Currency":"usd",
    "Paid":true,
    "List":{
        "Object":"list",
        "Data":[],
        "TotalCount":0
    },
    "Source":{
        "Type":0,
        "Card":{
            "Object":"card",
            "Brand":"Visa",
            "ExpirationMonth":"7",
            "ExpirationYear":"2023",
            "AddressCity":null,
            "AddressCountry":null,
            "Metadata":{},
            "Name":"Lion Mtosh"
        },
        "BankAccount":null,
        "OtherDetails":{}
    }
}

How do I retrieve Created date from this string?

var data = (JObject)JsonConvert.DeserializeObject(jsonString);
data["Created"] does not return date value.
41 72 6c
  • 1,600
  • 5
  • 19
  • 30
Happy Singh
  • 95
  • 2
  • 9
  • please pay attention to [code formatting](https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks) – jps Jul 07 '21 at 11:07
  • 1
    `data["Created"]` does return a date value, you can use even use `.Value();` to convert it to DateTime. The json you posted is missing a closing bracket by the way. – oRoiDev Jul 07 '21 at 11:12

3 Answers3

4

You can use dynamic object like this

dynamic results = JsonConvert.DeserializeObject<dynamic>(json);
var id = results.Id;
var name= results.Name;
3

I works for me.

https://dotnetfiddle.net/Widget/p6U5GY

using System;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
                    
public class Program
{
    public static void Main()
    {
        
        var jsonString = "{\"Object\":\"order\",\"LiveMode\":true,\"Amount\":3000,\"Captured\":false,\"Created\":\"/Date(1593359683000)/\",\"Currency\":\"usd\",\"Paid\":true,\"List\":{\"Object\":\"list\",\"Data\":[],\"TotalCount\":0},\"Source\":{\"Type\":0,\"Card\":{\"Object\":\"card\",\"Brand\":\"Visa\",\"ExpirationMonth\":\"7\",\"ExpirationYear\":\"2023\",\"AddressCity\":null,\"AddressCountry\":null,\"Metadata\":{},\"Name\":\"Lion Mtosh\"},\"BankAccount\":null,\"OtherDetails\":{}}}";
        
        var data = (JObject)JsonConvert.DeserializeObject(jsonString);
        
        Console.WriteLine(data["Created"]);
    }
}

Note: you are missing a closing paren at the end of JSON string.

golakwer
  • 355
  • 1
  • 4
-1

Define entity class contains same values name whith the same types and then make DeserializeObject in the entity

Public class ObjectClass { public int LiveMode {set;get;} . . .

} ObjectClass obj = ((Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(jsonText)).ToObject();

Ahmad naser
  • 119
  • 1
  • 3