Developer
Developer

Reputation: 4321

Handling JSON in windows phone 7

I have a JSON file coming from the internet:

string JSON = reader.ReadToEnd();

It works perfectly, the problem is here:

NewsList = JsonConvert.DeserializeObject<News>(JSON) as News;

This works perfectly on WP8 project, but WP7 somewhy fails it. Here is my News class:

    class News
    {
        [JsonProperty("jsonrpc")]
        public string Jsonrpc { get; set; }
        [JsonProperty("id")]
        public string Id { get; set; }
        [JsonProperty("result")]
        public Result Result { get; set; }
    }
    public class Result
    {
        private List<Article> articles = new List<Article>();
        [JsonProperty("articles")]
        public List<Article> Articles { get { return articles; } }
    }
    public class Article
    {
        [JsonProperty("text")]
        public string Text { get; set; }
        [JsonProperty("id")]
        public int Id { get; set; }
        [JsonProperty("date")]
        public long Date { get; set; }
        [JsonProperty("title")]
        public string Title { get; set; }
        [JsonProperty("author")]
        public string Author { get; set; }
        [JsonProperty("imageURL")]
        public string ImageURL { get; set; }
    }

Here is the error:

Attempt to access the method failed: WP7RTURelease2.HomePage+News..ctor()

System.MethodAccessException was unhandled Message=Attempt to access the method failed: WP7RTURelease2.HomePage+News..ctor() StackTrace: at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark) at System.Reflection.ConstructorInfo.Invoke(Object[] parameters) at Newtonsoft.Json.Utilities.LateBoundReflectionDelegateFactory.<>c_DisplayClass6`1.b_5() at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultConstructor) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value) at WP7RTURelease2.HomePage.HandleResponseNews(IAsyncResult result) at System.Net.Browser.ClientHttpWebRequest.<>c_DisplayClassa.b_8(Object state2) at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadPool.WorkItem.doWork(Object o) at System.Threading.Timer.ring()

Upvotes: 0

Views: 341

Answers (1)

Brian Rogers
Brian Rogers

Reputation: 129687

Try making your News class public.

Upvotes: 1

Related Questions