json DeserializeObject异常

时间:2012-03-28 21:50:49

标签: c#

我正在尝试将json结果转换为对象集,但是我得到了一个例外。 这是myClass描述,反序列化json字符串和json字符串本身的方法。

string json  = {"Similar": {"Info":[{"Name" : "Harry Potter","Type" : "book","wTeaser" : "Harry Potter is a series of seven fantasy novels written by the British author J. K. Rowling. The books chronicle the adventures of a wizard, Harry Potter and his friends Ronald Weasley and Hermione Granger, all of whom are students at Hogwarts School of Witchcraft and Wizardry. The main story arc concerns Harry's quest to overcome the Dark wizard Lord Voldemort, whose aims are to become immortal, to conquer the wizarding world, subjugate non-magical people, and destroy all those who stand in his way, especially Harry Potter.Since the release of the first novel Harry Potter and the Philosopher's Stone on 30 June 1997, the books have gained immense popularity, critical acclaim and commercial success worldwide. The series has also had some share of criticism, including concern for the increasingly dark tone.","wUrl" : "http://en.wikipedia.org/wiki/Harry_Potter","yTitle" : "","yUrl" : "","yID" : ""}],"Results":[{"Name" : "A Very Potter Musical","Type" : "movie","wTeaser" : "The immense popularity and wide recognition of J. K. Rowling's Harry Potter fantasy series has led to its being extensively parodied, in works spanning nearly every medium. The franchise holds the record for the most fan fiction parodies, at over 500,000. Some self-described parodies have been targeted by Rowling and her publishers as plagiarism, while others have sold hundreds of thousands of copies without any threat of legal sanction. Misinterpretations of Harry Potter parodies have sparked at least two urban legends. Many Harry Potter parodies are self-published; others are put out as part of major comic productions, such as Mad, The Simpsons, South Park, Saturday Night Live and Robot Chicken, all of which have parodied Harry Potter several times.","wUrl" : "http://en.wikipedia.org/wiki/Parodies_of_Harry_Potter","yTitle" : "A Very Potter Musical Trailer","yUrl" : "http://www.youtube.com/v/5DFQPY1FAG8?version=3&f=videos&c=TasteKid&app=youtube_gdata","yID" : "5DFQPY1FAG8"},{"Name" : "Percy Jackson & The Olympians","Type" : "book","wTeaser" : "Percy Jackson & the Olympians is a pentalogy of adventure and fantasy fiction books authored by Rick Riordan. Set in the United States, the books are predominantly based on Greek mythology and deals with a demigod, Percy Jackson and his quest to stop the rise of the Titan lord Kronos. All 5 books are seen in Percy's witty and sardonic point of view. The series tackles themes like coming of age, love and teenage angst.More than 20 million copies of the books have been sold in more than 10 countries.As of October 28, 2011, the series has been on the New York Times best seller list for children's book series for 223 weeks. The first book is adapted to a film, Percy Jackson & the Olympians: The Lightning Thief which received commercial success but mixed to negative reviews.","wUrl" : "http://en.wikipedia.org/wiki/Percy_Jackson_%26_The_Olympians","yTitle" : "","yUrl" : "","yID" : ""},{"Name" : "Harry Potter","Type" : "movie","wTeaser" : "The Harry Potter film series is a British-American film series based on the Harry Potter novels by the British author J. K. Rowling. The series is distributed by Warner Bros. and consists of eight fantasy films beginning with Harry Potter and the Philosopher's Stone (2001) and culminating with Harry Potter and the Deathly Hallows – Part 2 (2011). It is the highest-grossing film series of all-time in inflation unadjusted dollars, with $7.7 billion in worldwide receipts. Each film is in the 35 highest-grossing films of all-time in inflation unadjusted dollars.The series was produced by David Heyman and stars Daniel Radcliffe, Rupert Grint and Emma Watson as the three leading characters, Harry Potter, Ron Weasley and Hermione Granger.","wUrl" : "http://en.wikipedia.org/wiki/Harry_Potter_(film_series)","yTitle" : "Harry Potter and the Deathly Hallows Trailer Official HD","yUrl" : "http://www.youtube.com/v/_EC2tmFVNNE?version=3&f=videos&c=TasteKid&app=youtube_gdata","yID" : "_EC2tmFVNNE"},{"Name" : "Artemis Fowl","Type" : "book","wTeaser" : "Artemis Fowl is a series of eight science fantasy novels written by Irish author Eoin Colfer, starring the teenage criminal mastermind Artemis Fowl II. Colfer summed up the series as: \"Die Hard with fairies.\" There are seven novels in the series; the first was published in 2001 and the seventh was released in 2010. The eighth and final book is set to be released in 2012. A graphic novel was released in 2007, and a second in 2009. A third graphic novel and a movie are currently in the writing process. Series OverviewArtemis Fowl is the first book in the series. Artemis Fowl, the main character and anti-hero, and his bodyguard, Butler, kidnap LEPrecon Captain Holly Short, a fairy elf, to ransom her to \"the People,\" the various fairies who have moved their entire civilisation underground to hide from humans, for one ton of twenty-four carat gold.","wUrl" : "http://en.wikipedia.org/wiki/Artemis_Fowl_(series)","yTitle" : "","yUrl" : "","yID" : ""},{"Name" : "A Series Of Unfortunate Events","Type" : "book","wTeaser" : "A Series of Unfortunate Events is a series of children's novels (or novellas) by Lemony Snicket (the pen name of American author Daniel Handler) which follows the turbulent lives of Violet, Klaus, and Sunny Baudelaire after their parents' death in an arsonous house fire. The children are placed in the custody of their distant cousin Count Olaf, who begins to abuse them and openly plots to embezzle their inheritance. After the Baudelaires are removed from his care by their parents' estate executor, Arthur Poe, Olaf begins to doggedly hunt the children down, bringing about the serial slaughter and demise of a multitude of characters.The entire series is actively narrated by Snicket, who makes numerous references to his mysterious, deceased love interest, Beatrice.","wUrl" : "http://en.wikipedia.org/wiki/A_Series_of_Unfortunate_Events","yTitle" : "","yUrl" : "","yID" : ""},{"Name" : "Eragon","Type" : "book","wTeaser" : "Eragon is the first book in the Inheritance Cycle by Christopher Paolini, who began writing at the age of 15. After writing the first draft for a year, he spent a second year rewriting it and fleshing out the story and characters. Paolini's parents saw the final manuscript and decided to self-publish Eragon. Paolini spent a year traveling around the United States promoting the novel. By chance, the book was discovered by Carl Hiaasen, who got it re-published by Alfred A. Knopf. The re-published version was released on August 26, 2003.The book tells the story of a young farm boy named Eragon, who finds a mysterious stone in the mountains. A dragon he later names Saphira hatches from the stone, which was really an egg. When the evil King Galbatorix finds out about Eragon and his dragon, he sends his servants, the Ra'zac, after them in an effort to capture them.","wUrl" : "http://en.wikipedia.org/wiki/Eragon","yTitle" : "","yUrl" : "","yID" : ""},{"Name" : "Harry Potter And The Deathly Hallows","Type" : "movie","wTeaser" : "Deathly Hallows or Harry Potter and the Deathly Hallows is a novel by J. K. Rowling.Deathly Hallows may also refer to:","wUrl" : "http://en.wikipedia.org/wiki/Harry_Potter_and_the_Deathly_Hallows_(film)","yTitle" : "Harry Potter and the Deathly Hallows Trailer Official HD","yUrl" : "http://www.youtube.com/v/_EC2tmFVNNE?version=3&f=videos&c=TasteKid&app=youtube_gdata","yID" : "_EC2tmFVNNE"},{"Name" : "Darren Criss","Type" : "music","wTeaser" : "Darren Everett Criss (born February 5, 1987) is an American actor, singer, songwriter, instrumentalist, and composer. He is also a founding member and co-owner of StarKid Productions, a media and musical theater production company based in Chicago. He is best known for portraying Blaine Anderson, an openly gay high school student, on the FOX television series Glee.Criss first garnered attention playing the lead role of Harry Potter in StarKid's musical productions of A Very Potter Musical and A Very Potter Sequel. In January 2012, he made his Broadway debut, starring as J. Pierrepont Finch in the revival of How to Succeed in Business Without Really Trying. Early life and educationCriss was born in San Francisco, California, the youngest son of Cerina (nee Bru) and Charles William Criss, a banker and a former director of the San Francisco Opera, the Philharmonia Baroque Orchestra, the Stern Grove Festival, and San Francisco Performances.","wUrl" : "http://en.wikipedia.org/wiki/Darren_Criss","yTitle" : "Matthew Morrison & Darren Criss","yUrl" : "http://www.youtube.com/v/HlL7sxX4rUY?version=3&f=videos&c=TasteKid&app=youtube_gdata","yID" : "HlL7sxX4rUY"},{"Name" : "The Hunger Games","Type" : "book","wTeaser" : "The Hunger Games is a young adult novel written by Suzanne Collins. It was originally published in hardcover on September 14, 2008, by Scholastic. It is written in first person and introduces sixteen-year-old Katniss Everdeen, who lives in a post-apocalyptic world in the country of Panem where the countries of North America once existed. The Capitol, a highly advanced metropolis, holds absolute power over the rest of the nation. The Hunger Games are an annual event in which one boy and one girl aged 12 to 18 from each of the 12 districts surrounding the Capitol are selected by lottery to compete in a televised battle in which only one person can survive.The book has been released as a paperback and also an audiobook and e-book. The Hunger Games had an initial print of 200,000 – twice doubled from the original 50,000.","wUrl" : "http://en.wikipedia.org/wiki/The_Hunger_Games","yTitle" : "","yUrl" : "","yID" : ""},{"Name" : "The Chronicles Of Narnia","Type" : "book","wTeaser" : "The Chronicles of Narnia is a series of seven high fantasy novels by C. S. Lewis. It is considered a classic of children's literature and is the author's best-known work, having sold over 100 million copies in 47 languages. Written by Lewis between 1949 and 1954, illustrated by Pauline Baynes and originally published in London between October 1950 and March 1956, The Chronicles of Narnia has been adapted several times, complete or in part, for radio, television, the stage, and film.Set in the fictional realm of Narnia, a place where animals talk, magic is common, and good battles evil, the series narrates the adventures of various children who play central roles in the unfolding history of that world. Except in The Horse and His Boy, the protagonists are all children from the real world magically transported to Narnia, where they are called upon by the lion Aslan to protect Narnia from evil and restore the throne to its rightful line.","wUrl" : "http://en.wikipedia.org/wiki/The_Chronicles_of_Narnia","yTitle" : "","yUrl" : "","yID" : ""},{"Name" : "His Dark Materials","Type" : "book","wTeaser" : "His Dark Materials is a trilogy of fantasy novels, coming together to form an epic, by Philip Pullman comprising Northern Lights (1995, published as The Golden Compass in North America), The Subtle Knife (1997), and The Amber Spyglass (2000). It follows the coming-of-age of two children, Lyra Belacqua and Will Parry, as they wander through a series of parallel universes against a backdrop of epic events. The three novels have won various awards, most notably the 2001 Whitbread Book of the Year prize, won by The Amber Spyglass. Northern Lights won the Carnegie Medal for children's fiction in the UK in 1995. The trilogy as a whole took third place in the BBC's Big Read poll in 2003.The story involves fantasy elements such as witches and armoured polar bears, and alludes to a broad range of ideas from such fields as physics, philosophy, and theology.","wUrl" : "http://en.wikipedia.org/wiki/His_Dark_Materials","yTitle" : "","yUrl" : "","yID" : ""},{"Name" : "The Hunger Games","Type" : "movie","wTeaser" : "The Hunger Games is an upcoming science fiction action-drama film directed by Gary Ross and based on the novel of the same name by Suzanne Collins. It stars Jennifer Lawrence, Josh Hutcherson, Elizabeth Banks, Liam Hemsworth, and Woody Harrelson. It is scheduled to be released on March 23, 2012. PlotAt an unidentified future date, the nation of Panem has risen out of the ruins of what was once known as North America. Due to an unsuccessful uprising by the twelve districts of Panem, a raffle (known as the 'reaping') is held to choose one boy and girl, aged 12–18, from each district to risk their lives in the Hunger Games, a ruthless competition in which each contestant, or tribute, tries to kill the others until only one is left alive.","wUrl" : "http://en.wikipedia.org/wiki/The_Hunger_Games_(film)","yTitle" : "The Hunger Games Theatrical Trailer #2","yUrl" : "http://www.youtube.com/v/qoUT7q2iTbQ?version=3&f=videos&c=TasteKid&app=youtube_gdata","yID" : "qoUT7q2iTbQ"},{"Name" : "Eldest","Type" : "book","wTeaser" : "Eldest is the second book in the Inheritance Cycle by Christopher Paolini and the sequel to Eragon. Eldest was first published in hardcover on August 23, 2005, and was released in paperback in September 2006.Eldest has been released in an audiobook format, and as an ebook. Like Eragon, Eldest became a New York Times bestseller. A deluxe edition of Eldest was released on September 26, 2006, including new information and art by both the illustrator and the author. Other editions of Eldest are translated into different languages.Eldest begins following several important events in Eragon. The story is the continued adventures of Eragon and his dragon Saphira, centering around their journey to the realm of the Elves in order to further Eragon's training as a Dragon Rider.","wUrl" : "http://en.wikipedia.org/wiki/Eldest","yTitle" : "","yUrl" : "","yID" : ""},{"Name" : "Inkheart","Type" : "book","wTeaser" : "Inkheart (original title: Tintenherz) is a young adult-child fantasy novel by Cornelia Funke, and the first book of the Inkworld trilogy.Inkheart follows the adventures of a 12-year-old girl named Meggie Folchart whose life changes dramatically when she discovers that her father, a bookbinder named Mortimer (Mo), has an unusual ability; when he reads aloud, he can bring characters and items from books into the real world. When Meggie was three years old, Mo read a book called Inkheart aloud to her mother. In an instant, Meggie's mother (and two cats) vanished into the Inkworld and three men from the novel (two of whom are murderous villains) entered into the real world. Nine years later, these men have come back into their lives and Meggie's and Mo's lives will never be the same.","wUrl" : "http://en.wikipedia.org/wiki/Inkheart","yTitle" : "","yUrl" : "","yID" : ""},{"Name" : "The Mortal Instruments","Type" : "book","wTeaser" : " Publication historyThe Mortal Instruments is a series of six young adult science fiction and fantasy novels written by Cassandra Clare, of which four have been published so far.Clary Fray and Simon Lewis are at a club where Clary witnesses a murder by a group of teens. She is shocked that Simon and the club's security guard cannot see the killer—a boy called Jace, who claims that the boy he murdered was a demon. The next day, Jace offers to take Clary to meet his tutor, but before she can do so, she receives a distressing phone call from her mother. She returns home to find her mother missing, the apartment trashed, and a monstrous creature, a Ravener demon, lying in wait for her. She defeats the creature by shoving Jace’s Sensor down its throat, but is injured in the process, and Jace takes her to his home.","wUrl" : "http://en.wikipedia.org/wiki/The_Mortal_Instruments_(series)","yTitle" : "","yUrl" : "","yID" : ""},{"Name" : "The Woman In Black","Type" : "movie","wTeaser" : "The Woman in Black is a 2012 supernatural horror-thriller film directed by James Watkins and written by Jane Goldman, and is based on Susan Hill's novel of the same name. It is produced by Hammer Film Productions. The film stars Daniel Radcliffe, Ciaran Hinds, Janet McTeer, Sophie Stuckey, and Liz White. It was released in the United States and Canada on 3 February 2012 to generally positive reviews, and was released in the United Kingdom on 10 February 2012. PlotThe film opens with a shot of three girls having tea and playing with dolls, who then simultaneously look at a corner of the room, and then immediately get up and jump out a window, committing suicide as their mother screams outside. In the Edwardian era, young solicitor Arthur Kipps (Daniel Radcliffe) lives with his four-year-old son, Joseph (Misha Handley) and his son's nanny (Jessica Raine).","wUrl" : "http://en.wikipedia.org/wiki/The_Woman_in_Black_(2012_film)","yTitle" : "The Woman In Black - Official Trailer [HD]","yUrl" : "http://www.youtube.com/v/7lReemWmO5o?version=3&f=videos&c=TasteKid&app=youtube_gdata","yID" : "7lReemWmO5o"},{"Name" : "Catching Fire","Type" : "book","wTeaser" : "Catching Fire is the second book in The Hunger Games Trilogy by Suzanne Collins. As the sequel to the 2008 bestseller The Hunger Games, it continues the story of Katniss Everdeen and the fictional, futuristic nation of Panem. Following the events of the previous novel, a rebellion against the oppressive Capitol has begun, and Katniss and fellow tribute Peeta are forced to return to the arena in a special edition of the Hunger Games.The book was released September 1, 2009, and was later released in eBook and audiobook format. Major themes include survival, government control, rebellion and interdependence vs. independence. Catching Fire has received mostly positive reviews. Reviewers praised the prose, the ending, and how Katniss became more sophisticated.","wUrl" : "http://en.wikipedia.org/wiki/Catching_Fire_(2009_novel)","yTitle" : "","yUrl" : "","yID" : ""}]}}



public class myType
{
    public string Name { get; set; }

    public string Type { get; set; }

    public string wTeaser { get; set; }

    public string wUrl { get; set; }

    public string yTitle { get; set; }

    public string yUrl { get; set; }

    public string yID { get; set; }

}

public List<myType> parseToObjMyType(String json)
{
    JObject movieSearch = JObject.Parse(json);
    List<myType> searchResults = new List<myType>();
    // get JSON result objects into a list
    List<JToken> results = movieSearch["Similar"].Children().ToList();

    foreach (JToken result in results)
    {
        myType searchResult = JsonConvert.DeserializeObject<myType>(result.ToString());
        searchResults.Add(searchResult);
    }

    return searchResults;

}

有什么不对?

1 个答案:

答案 0 :(得分:1)

假设您正在使用Json.Net,我会这样做(使用dynamics):

dynamic jObj = JsonConvert.DeserializeObject(json);
Console.WriteLine(jObj.Similar.Info[0].Name + "\n");
foreach (var result in jObj.Similar.Results)
{
    Console.WriteLine("{0}:{1}",result.Type, result.Name);
}

输出将是:

Harry Potter

movie:A Very Potter Musical
book:Percy Jackson & The Olymp
movie:Harry Potter
book:Artemis Fowl
book:A Series Of Unfortunate E
book:Eragon
movie:Harry Potter And The Dea
music:Darren Criss
book:The Hunger Games
book:The Chronicles Of Narnia
book:His Dark Materials
movie:The Hunger Games
book:Eldest
book:Inkheart
book:The Mortal Instruments
movie:The Woman In Black
book:Catching Fire