-1

Binding does not work in my code. what's wrong in this code?

HttpClient client = new HttpClient();
var response = await client.GetAsync(string.Format("uri link"));
string jsonstring = await response.Content.ReadAsStringAsync();
RootObject item = JsonConvert.DeserializeObject<RootObject>(jsonstring);
titles.ItemsSource =item.ToString();

XAML code

<ListView x:Name="titles" HasUnevenRows="False" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <StackLayout Orientation="Horizontal">
                           <Label Text="{Binding note}"/>
                       </StackLayout>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

class object:

public class Result
{
    public string note { get; set; }
}
public class Response
{
    public List<Result> results { get; set; }
}
public class RootObject
{
    public Response response { get; set; }
}
James Z
  • 12,209
  • 10
  • 24
  • 44

2 Answers2

1

you bind the lable to the note, but you set the titles.ItemsSource to the RootObject. the RootObject class doesn't have note. note is in Result class.

and you can't set the itemsource like that.

I suggest you to do this

var listItem = JsonConvert.DeserializeObject<List<Result>>(jsonstring);
titles.ItemsSource = l;
Sajjad Abedi
  • 122
  • 2
  • 9
  • if i am going to try this code it shows some error :-- Cannot deserialize the current json object. To fix this error either change the json or json array. whats wrong in this code. – Muthukumar c Apr 02 '18 at 17:30
  • I think its because your jsonstring contains more than one instance of Result class. I mean it has more than one note property. Am I right? – Sajjad Abedi Apr 02 '18 at 19:16
  • Hi @MSjjD jsonnstring : { "response": { "results": [ { "Created By": "non_authenticated_user_live", "Created Date": "2018-03-05T08:34:04.631Z", "Modified Date": "2018-03-05T08:34:04.677Z", "note": "gmmrng", "_id": "1520238844627x475879674311727300", "_type": "custom.note" } – Muthukumar c Apr 03 '18 at 14:26
  • your jsonnstring in not complete. – Sajjad Abedi Apr 05 '18 at 09:19
1

According to me are upto:

RootObject item = JsonConvert.DeserializeObject<RootObject>(jsonstring);

and can you try this code also after the above line:

titles.ItemsSource =item.Responce. results;
Rence
  • 2,900
  • 2
  • 23
  • 40