Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Json array to Picker in Xamarin Forms

$
0
0

Hello,

What Im trying to accomplish: scan the qr code -> get the data from the api -> display that in picker.

For past 2 days Im trying to accomplish following thing:

1)Get json data from my api
2) Display that to picker

Here is my JSON:

{ "20": [ ["25:1222:118.00"], ["40:1224:121.60"], ["50:1232:124.00"], ["80:1233:131.20"] ], "16": [ ["125:1223:116.00"], ["150:1225:119.00"], ["160:1226:120.20"], ["120:1538:115.40"] ], etc. etc. etc.

"20 and 16" I need to display as unique values in my Picker, while the thing I actually want to display INSIDE (the thing you pick) the picker is [this->125:1223:116.00] Thats stroke of the pneumatic cylinder, next number is ID and the last one is price.

Here is my Model I currently have and here is where my problem is:

` public class Silowniki
{
//public string Dia { get; set; }
public string Stroke { get; set; }
public string Id { get; set; }
public string Cena { get; set; }

}

public class Diameter
{
    public List<Silowniki> diameter { get; set; }
}`

My ViewModel

`public class ScannedViewModel : INotifyPropertyChanged
{
public int Quantity { get; set; }
public ObservableCollection Silowniki { get; set; }

    public string Test { get; set; }
    public Command AddTest { get; set; }
    public Command LogOut { get; set; }
    //public Command PokazListe { get; set; }

    public ScannedViewModel()
    {
        Silowniki = new ObservableCollection<Silowniki>();

        //PokazListe = new Command(() => _PokazListe(Silowniki));
        AddTest = new Command(() => TestowyProdukt(ItemOb));
        LogOut = new Command(() => _LogOut());
    }

}`

And finally (i dont know if this is the proper way) here is the code which I have after I receive the data from the api:

`ScannerPage.OnScanResult += (result) =>
{
ScannerPage.IsScanning = false;
Device.BeginInvokeOnMainThread(async () =>
{
await Navigation.PopAsync();
dynamic jsonRespone = await ConnectWithOauth.GetRequest(result.Text);
JObject parsedJson = JObject.Parse(jsonRespone);
var kk = JsonConvert.DeserializeObject(jsonRespone);
Console.WriteLine(kk);
//This below doesnt work, wanted to check if there are any elements in it..
/*
foreach (var item in kk)
{
Console.WriteLine("Pokazuje item:\n");
Console.WriteLine(item);
}
*/
SaveProducts(parsedJson, viewModel);
//My save products function
async public void SaveProducts(dynamic json, ScannedViewModel model)
{
foreach (var item in json)
{
foreach (var prop in item)
{
foreach (var test in prop)
{
Silownik = new Silowniki
{
Stroke = Convert.ToString(test[0]).Split(':')[0],
Id = Convert.ToString(test[0]).Split(':')[1],
Cena = Convert.ToString(test[0]).Split(':')[2]
};

        //Here is what Idk what to do
                    MyDia = new Diameter
                    {
                        //diameter = 
                    };
                    //Console.WriteLine(Silownik.Dia + ":" + Silownik.Cena);
                    try
                    {
                        model.ItemOb.Add(Item);
                        model.Silowniki.Add(Silownik);
                    }
                    catch (Exception e)
                    {
                        Debug.Write(e);
                    }
                }
            }
        }
        ScannedProducts nextPage = new ScannedProducts(model);
        nextPage.BindingContext = model;
        await Navigation.PushAsync(nextPage);~~~~
    }`

My function where Im getting the data:
if (response.StatusCode == HttpStatusCode.OK) { Console.WriteLine("json response"); var a = JsonConvert.DeserializeObject(responseText); return a;

I dont know what to do now.. Im really stuck.. I know how to display things from the viewModel in the xaml view, but if Im getting them in json format as I want them to be displayed I doubt if this is the right thing to do (looping through every element and creating it in the viewmodel)

Also Idk what this does: var kk = JsonConvert.DeserializeObject(jsonRespone); and how can I display it in my xaml view

From what ive read on the internet and forum here my xaml should be something like that:
<StackLayout x:Name="myStackLayout"> <Label x:Name="idKlienta" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" /> <Picker Title="Wybierz Diameter" ItemsSource="{Binding Silowniki}" ItemDisplayBinding="{Binding Dia}"> </Picker> <Picker Title="Skok" ItemsSource="{Binding Silowniki}" ItemDisplayBinding="{Binding Id}"> </Picker></stacklayout..>

Im stuck please help me.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>