【问题标题】:Get value from JSON with JSON.NET使用 JSON.NET 从 JSON 中获取价值
【发布时间】:2012-01-25 20:47:14
【问题描述】:

我尝试使用http://www.codeplex.com/Json从一个json对象中提取值。

我使用 imdbapi.com,它们返回的 json 格式如下:

{"Title": "Star Wars", "Year": "1977", "Rated", "PG", "Released", "25 May 1977", "Genre", "Action, Adventure, Fantasy, Sci-Fi "" Director ":" George Lucas "," Writer "," George Lucas "," Actors ":" Mark Hamill, Harrison Ford, Carrie Fisher, Alec Guinness, "" Plot ":" Luke Skywalker leaves his home planet, teams up With Other Rebels, and Tries to save Princess Leia from the evil clutch of Darth hrs 1 min "," Rating ":" 8.8 "," Votes ":" 397318 "," ID ":" tt0076759 "," Response ":" True "}

我怎样才能得到这样的标题、等级、年份?并将其保存到我的对象中?

此行返回正确的 json:

JObject jObject = JObject.Parse (json);

现在我只需要帮助挑选我想要的数据。有什么建议吗?

【问题讨论】:

    标签: c# json


    【解决方案1】:

    这应该可以帮助你http://james.newtonking.com/pages/json-net.aspx

    string json = @"{
        ""Name"": ""Apple"",
        ""Expiry"": new Date(1230422400000),
        ""Price"": 3.99,
        ""Sizes"": [
            ""Small"",
            ""Medium"",
            ""Large""
        ]
    }";
    
    JObject o = JObject.Parse(json);
    
    //This will be "Apple"
    string name = (string)o["Name"];
    

    【讨论】:

    • 你应该测试是否密钥:o.ContainsKey("Name")
    【解决方案2】:

    JObject API documentation

    相信您对返回 JTokens 的 .Item[key] 集合感兴趣。

    Full Documentation

    【讨论】:

      【解决方案3】:
      class TypeHere{
         string Name {get;set;}
      }
      
      TypeHere object = JsonConvert.DeserializeObject< TypeHere >(jsonString)
      
      // Ex. output 
      object.Name;
      

      【讨论】:

        猜你喜欢
        • 2013-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-09
        • 2019-07-20
        • 1970-01-01
        相关资源
        最近更新 更多