0

I'm new to Xamarin and using JSON as well.

I'm using an REST API from our ERP system to get the data, and I'm able to do so without problem.

My problem is that I'm unable to figure out how to get the data into a list view correctly. I've tried several threads on stack overflow and I'm not having any success.

Here is the JSON data below:

{
  "odata.metadata":"https://ERPSERVER&$select=PartNum,%20PartDescription","value":[
    {
      "PartNum":"A1","PartDescription":"A1DescHere"
    },{
      "PartNum":"A2","PartDescription":"A2DescHere"
    }
  ]
}

Here is the XAML code for the list

        <ListView x:Name="PartList" HasUnevenRows="true">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding PartNum}" Detail="{Binding PartDescription}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

I am running in circles, here is some of the code I have tested:

            string str = json.Substring(json.IndexOf("["));
            string str2 = str.Replace("[", "").Replace("]","").TrimEnd('}');

            Part part = JsonConvert.DeserializeObject<Part>(str2);

            PartNum.Text = part.PartNum;
            PartDescription.Text = part.PartDescription;

I had to use substring and replace to remove some of the odata to get the DeserializeObject correctly.

Can anyone help point me in the right direction?

Thanks all!

1
  • 3
    use json2csharp.com to generate a C# model to match your data and then deserialize directly to that. String manipulation on json is a bad idea Commented Jan 3, 2020 at 22:00

1 Answer 1

3

use json2csharp.com to build a model

public class Value
{
    public string PartNum { get; set; }
    public string PartDescription { get; set; }
}

public class RootObject
{
    public string __invalid_name__odata.metadata { get; set; }
    public List<Value> value { get; set; }
}

then deserialize it

var parts = JsonConvert.DeserializeObject<RootObject>(json);

and then assign your ItemsSource

PartList.ItemsSource = parts.value;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.