如何将动态Json解析为字符串?

时间:2017-01-07 15:25:02

标签: c# json json.net

我在www.jsonlint.com上查看了以下有效Json。以下是Json的一部分,完整的Json太大了,无法在此处发布。

{
    "data": [{
        "name": "Micheal Jackson",
        "pic_large": "https://scontent.x.fbcdn.net/v/t1.0-1/p200x200/14909900_10154513795037597_3241587822245799922_n.jpg?oh=54ead7e0ba74b45b632d96da1515ccf8&oe=591C4938",
        "id": "10154729171332597"
    }]
}

当我想使用JsonConvert对其进行反序列化时,我会在调用以下代码时出现以下错误:

  

"无法隐式转换类型' Facebook.JsonArray'到'字符串'"。

string json = myFeed.data;
var result = JsonConvert.DeserializeObject<Result>(json);

有没有办法将Json动态对象反序列化为字符串?

1 个答案:

答案 0 :(得分:0)

我这样做的方法是创建一个名为data的类,实例变量名为name,pic_large为id,然后应用以下使用JsonConvert.SerializeObject和JsonConvert.DeserializeObject的方法

library(ggplot2)
library(plotly)

shipNames <- c("Princess", "Voyager", "Paul Simon", "Dangelo", "Virginia", "Meade", "Capt America", "Naw Dawg")

set.seed(1)
cruise <- data.frame(company = sample(c("Carnival", "Norweigen"), 20, replace = T),
                     shipName = sample(shipNames, 20, replace=T),
                     date = sample(seq(from=as.Date('2016-01-01'), to=as.Date('2016-12-31'), by=1), 20),
                     distance = sample(seq(from=300, to=500, by=1), 20)
                     )

g <- ggplot(cruise, aes(x=date, y=distance, colour=company, name = shipName)) + geom_point()
# you have to put the geom_line first or it fucks it up
g <- ggplot(cruise, aes(x=date, y=distance)) + geom_line(aes(colour=company)) + geom_point(aes(colour=company, name = shipName))

ggplotly(g, tooltip = c("x", "name"))

了解更多信息 http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConvert.htm