Deserealization无法正常工作

时间:2015-06-24 12:09:56

标签: c# json

我已经为反序列化this JSON

创建了一个类
filter = ones(3)/9;
filtr_image = uint8(conv2(noisy_image, filter, 'same'));

所以我创建了一个函数来创建一个HttpRequest和相对对象:

public class Self
{
    public string href { get; set; }
}

public class Team
{
    public string href { get; set; }
}

public class Links
{
    public Self _self { get; set; }
    public Team team { get; set; }
}

public class Player
{
    public int id { get; set; }
    public string name { get; set; }
    public string position { get; set; }
    public int jerseyNumber { get; set; }
    public string dateOfBirth { get; set; }
    public string nationality { get; set; }
    public string contractUntil { get; set; }
    public string marketValue { get; set; }
}

public class RootObject
{
    public Links _links { get; set; }
    public int count { get; set; }
    public List<Player> players { get; set; }
}

public struct Player_Struct
{
    public string id;
    public string name;
    public string position;
    public int jerseyNumber;
    public string dateOfBirth;
    public string nationality;
    public string contractUntil;
    public string marketValue;
}

现在问题是编译器返回此异常:

  

未处理的异常&#39; Newtonsoft.Json.JsonSerializationException&#39;在Newtonsoft.Json.dll

     

Ulteriori informazioni:无法将当前JSON数组(例如[1,2,3])反序列化为类型&#39; SF_DebugProject.API.Players + Links&#39;因为该类型需要一个JSON对象(例如{&#34; name&#34;:&#34; value&#34;})才能正确反序列化。

     

要修复此错误,请将JSON更改为JSON对象(例如{&#34; name&#34;:&#34; value&#34;})或将反序列化类型更改为数组或实现的类型像List这样的集合接口(例如ICollection,IList)可以从JSON数组反序列化。 JsonArrayAttribute也可以添加到类型中,以强制它从JSON数组反序列化。

     

路径&#39; _links&#39;,第1行,第11位。

因此,通过修复提示,我已尝试修复错误,将对象更改为:

 string requestUrl = teams.link_teams;
 string responseText = parser.Request(requestUrl);
 var obj = JsonConvert.DeserializeObject<Players.RootObject>(responseText);

但是这样我在做foreach时就看不到rootobject的任何属性了。我做错了什么?

1 个答案:

答案 0 :(得分:1)

根据我的理解,您希望在包含播放器列表的RootObject中反序列化JSON字符串。

这些类应如下所示:

Dispose

所以你应该做的事情如下:

library(RXKCD)
library(tm)
library(wordcloud)
library(RColorBrewer)
require(gdata)

path <- system.file("xkcd", package = "RXKCD")
datafiles <- list.files(path)
xlsdf <- read.csv(file.path(path, datafiles))


ap.corpus <- Corpus(DataframeSource(data.frame(as.character(xlsdf[,'transcript'])))) 
ap.corpus <- tm_map(ap.corpus, removePunctuation) 
ap.corpus <- tm_map(ap.corpus, tolower) 
ap.corpus <- tm_map(ap.corpus, removeNumbers)
ap.corpus <- tm_map(ap.corpus, function(x) removeWords(x, stopwords("english"))) 
# additional stopwords can be used as shown below  
#ap.corpus <- tm_map(ap.corpus, function(x) removeWords(x, c("ukoer","oer"))) 
ap.corpus <- tm_map(ap.corpus, PlainTextDocument)
ap.tdm <- TermDocumentMatrix(ap.corpus) 
findFreqTerms(ap.tdm, lowfreq=40)
ap.m <- as.matrix(ap.tdm) 
ap.v <- sort(rowSums(ap.m),decreasing=TRUE) 
ap.d <- data.frame(word = names(ap.v),freq=ap.v) 
print(table(ap.d$freq) )
pal2 <- brewer.pal(8,"Dark2") 

# png("wordcloud_packages.png", width=1280,height=800) 
#print(wordcloud(ap.d$word,ap.d$freq, scale=c(8,.2),min.freq=40, 
#          max.words=Inf, random.order=FALSE, rot.per=.05, colors=pal2))
# dev.off()

f <- matrix (0, ncol=nrow(ap.tdm), nrow=nrow(ap.tdm))  
colnames (f) <- rownames(ap.tdm)
rownames (f) <- rownames(ap.tdm)

# This is the nested loop to replace
#for (i in rownames (ap.tdm)) { 
#  ff <- findAssocs (ap.tdm,i,0)
#  for  (j in rownames (ff)) {
#    f[j,i]=ff[j,]
#  }
#}

fcn2 <- function(j,ff) { ff[j]; }
fcn1 <- function(i) {ff<-findAssocs(ap.tdm,i,0); 
                     f[rownames(ff),i]<-lapply(rownames(ff), fcn2, ff);}
lapply(rownames(ap.tdm), fcn1)

fd <- as.dist(f) # calc distance matrix
plot(hclust(fd, method="ward"))  # plot dendrogram

# very simple dendrogram
hc = hclust(dist(f))
plot(hc)