none
change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal RRS feed

  • Question

  • Hi Devellopers can anybody help me with this i m using xamarin.forms

    i need to display the JSON result in the mobile interface for console it's works 

    i already convert my Json to C#  and paste it in new Class JSON1     from http://json2csharp.com/

    this is my code for Homepage.xaml.cs :

               dynamic JResponse_Student = JsonConvert.DeserializeObject<List<JSON1>>(result_Student.Content);
               ProductsListViews.ItemsSource = JResponse_Student;

    //Jresponse_Student Content the JSON deserialize 

    this is my code for Homepage.xaml : 

     <ListView x:Name="ProductsListViews">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">


                            <Label Text="{Binding year}" TextColor="Black"></Label>
                            <Label Text="{Binding name}" TextColor="Black"></Label>
                            <Label Text="{Binding surname}" TextColor="Black"></Label>
                            

                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

    but i get this error : 

    Unhandled Exception:

    Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object
     (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[University.Data.JSON1]'
     because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
    To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized 
    type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type l
    ike an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added 
    to the type to force it to deserialize from a JSON object.
    Path 'meta', line 1, position 8. a eu lieu 


    My Json is :

    {"meta":{"service":"student","row_count":18,"year":"2017","params":{"year":"2017","Params":{"service":"student","year":"2017","login_ldap_key_name":"LOGIN RESEAU","login_ldap_key_value":"aaubert","email_key_name":"EMAIL ECOLE","photo_key_name":"PHOTO","cellular_key_name":"PORTABLE"}}},"data":{"id":"AUBERT Antoine","name":"AUBERT","surname":"Antoine","login":"aaubert","mail":"antoine.aubert@epfedu.fr","photo":".jpg","cell_phone":"06 87 99 95 20","annee_universitaire":"2017\/2018","code_formation":"PSFGE1","lib1_formation":"Formation G\u00e9n\u00e9raliste 1\u00e8re Ann\u00e9e (Sceaux)","lib2_formation":"","base_url_photo":"http:\/\/mydata.epf.fr\/nfsdoc\/epf-etp\/ressources\/xfiles\/etudiants\/.jpg","personal_email":"","student_id":"","diplome_type":"INGENIEUR","diplome_code":"EPF FG","diplome_lib":"INGENIEUR Dipl\u00f4me d'ing\u00e9nieur g\u00e9n\u00e9raliste de l'EPF","administrative_status":""}}

    Friday, July 27, 2018 11:23 AM

All replies

  • The error you posted has nothing to do with the UI but your question seems to be oriented that way. The error is being caused by the fact that your JSON data doesn't line up with what you're trying to read back in. You have a serialization issue.

    In your DeserializeObject call you are specifying List<JSON1>. But the JSON itself is a single object so that won't work. Either the JSON being specified needs to be an array (wrap in []) or you need to change the type used in DeserializeObject to simply be JSON1 instead of List<JSON1>. Of course you may have other errors with your JSON but this will get you past the first issue.

    For questions related to Xamarin and the UI you need to post in their forums. We can only help with the C# stuff here.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, July 27, 2018 3:26 PM
    Moderator
  • Hi Benhaddou Soufiane,

    Since your question is more related to Xamarin, you could post a new thread in Xamarin forum for suitable support.

    https://forums.xamarin.com/

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 30, 2018 6:02 AM
    Moderator
  • One works with a collection of objects being returned, and the other one works with a single object being returned.

            public List<DtoTask> GetTasksByProjIdApi(int id)
            {
                // the method is being used synchronously
                var client = new HttpClient();
    
                var uri = new Uri("http://progmgmntcore2api.com/api/task/GetTasksByProjId?id=" + id);
    
                var response = client.GetAsync(uri).Result;
    
                var responseContent = response.Content;
                var responseString = responseContent.ReadAsStringAsync().Result;
    
                dynamic tasks = JArray.Parse(responseString) as JArray;
    
                var dtotasks = new List<DtoTask>();
                
                foreach (var obj in tasks)
                {
                    var dto = new DtoTask
                    {
                        TaskId = obj.taskId,
                        Note = obj.note,
                        TaskName = obj.taskName,
                        StartDate = obj.startDate,
                        EndDate = obj.endDate,
                        Status = obj.status,
                        TaskDuration = obj.taskDuration,
                        TaskSpent = obj.taskSpent,
                        ProjectId = obj.projectId,
                        ResourceId = obj.resourceId
                    };
    
                    dtotasks.Add(dto);
                }
    
                return dtotasks;
            }
    
            public DtoTask GetTaskByIdApi(int id)
            {
                // the method is being used synchronously
    
                var client = new HttpClient();
    
                var uri = new Uri("http://progmgmntcore2api.com/api/task/GetTaskById?id=" + id);
    
                var response = client.GetAsync(uri).Result;
    
                var responseContent = response.Content;
                var responseString = responseContent.ReadAsStringAsync().Result;
    
                dynamic task = JObject.Parse(responseString);
    
                var dtot = task.dtoTask;
    
                var dto = new DtoTask
                {
                    TaskId = dtot.taskId,
                    Note = dtot.note,
                    TaskName = dtot.taskName,
                    StartDate = dtot.startDate,
                    EndDate = dtot.endDate,
                    Status = dtot.status,
                    TaskDuration = dtot.taskDuration,
                    TaskSpent = dtot.taskSpent,
                    ProjectId = dtot.projectId,
                    ResourceId = dtot.resourceId
                };
    
                return dto;
            }
    

    Monday, July 30, 2018 6:33 AM