Benhaddou Soufiane
Benhaddou Soufiane

Reputation: 23

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

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>

The JSON Class :

class JSON1
{

    public class Params2
    {
        public string service { get; set; }
        public string year { get; set; }
        public string login_ldap_key_name { get; set; }
        public string login_ldap_key_value { get; set; }
        public string email_key_name { get; set; }
        public string photo_key_name { get; set; }
        public string cellular_key_name { get; set; }
    }

    public class Params
    {
        public string year { get; set; }
       // public Params2 Params { get; set; }
    }

    public class Meta
    {
        public string service { get; set; }
        public int row_count { get; set; }
        public string year { get; set; }
        public Params @params { get; set; }
    }

    public class Data
    {
        public string id { get; set; }
        public string name { get; set; }
        public string surname { get; set; }
        public string login { get; set; }
        public string mail { get; set; }
        public string photo { get; set; }
        public string cell_phone { get; set; }
        public string annee_universitaire { get; set; }
        public string code_formation { get; set; }
        public string lib1_formation { get; set; }
        public string lib2_formation { get; set; }
        public string base_url_photo { get; set; }
        public string personal_email { get; set; }
        public string student_id { get; set; }
        public string diplome_type { get; set; }
        public string diplome_code { get; set; }
        public string diplome_lib { get; set; }
        public string administrative_status { get; set; }
    }

    public class RootObject
    {
        public Meta meta { get; set; }
        public Data data { get; set; }
    }


}

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": "[email protected]",
    "photo": ".jpg",
    "cell_phone": "06 87 99 95 20",
    "annee_universitaire": "2017/2018",
    "code_formation": "PSFGE1",
    "lib1_formation": "Formation Généraliste 1ère Année (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ôme d'ingénieur généraliste de l'EPF",
    "administrative_status": ""
  }
}

Upvotes: 0

Views: 6320

Answers (2)

Domain Store
Domain Store

Reputation: 1

Just enclose your response with [] backet

Upvotes: 0

phuzi
phuzi

Reputation: 13059

Change

JsonConvert.DeserializeObject<List<JSON1>>(result_Student.Content);

to

JsonConvert.DeserializeObject<JSON1>(result_Student.Content);

The JSON you have included in the collection is not an array/collection/list but a single object.

Upvotes: 1

Related Questions