How to convert a JToken

后端 未结 4 1435
遇见更好的自我
遇见更好的自我 2021-02-19 13:15

I have a JToken with the value {1234}

How can I convert this to an Integer value as var totalDatas = 1234;

var tData = jObject[\"$totalDatas\"];
int tota         


        
相关标签:
4条回答
  • 2021-02-19 13:30

    You should use:

    int totalDatas = tData.Value<Int32>();
    
    0 讨论(0)
  • 2021-02-19 13:38

    try this: int value = (int)token.Value;

    0 讨论(0)
  • 2021-02-19 13:52

    You can simply cast the JToken to int :

    string json = @"{totalDatas : ""1234""}";
    JObject obj = JObject.Parse(json);
    JToken token = obj["totalDatas"];
    int result = (int)token;
    
    //print 2468
    Console.WriteLine(result*2);
    

    [.NET fiddle demo]

    0 讨论(0)
  • 2021-02-19 13:54

    You can use the JToken.ToObject<T>() method.

    JToken token = ...;
    int value = token.ToObject<int>();
    
    0 讨论(0)
提交回复
热议问题