1

I am getting json file from php script and it looks like this:

[ 
{"data": { "yaw":0.0, "roll":0.0, "pitch":0.0 }, "name":"acceleration", "unit":"deg" },
{"data": { "yaw":0.0, "roll":0.0, "pitch":0.0 }, "name": "gyro", "unit":"deg"}
]

So, I am making a ListView, which will show name, yaw, roll and pitch values and unit. I have made ListView already and my xaml looks like this:

<ListView Grid.Row="0" ItemsSource="{Binding OriMeasurements}">
    <ListView.View>
        <GridView>
           <GridViewColumn Header="Name" Width="100" DisplayMemberBinding="{Binding Name}"/>
               <GridViewColumn Header="Roll" Width="100" DisplayMemberBinding="{Binding Roll}"/>
               <GridViewColumn Header="Pitch" Width="100" DisplayMemberBinding="{Binding Pitch}"/>
               <GridViewColumn Header="Yaw" Width="100" DisplayMemberBinding="{Binding Yaw}"/>
               <!--<GridViewColumn Header="Data" Width="100" DisplayMemberBinding="{Binding Data}"/>-->
               <GridViewColumn Header="Unit" Width="100" DisplayMemberBinding="{Binding Unit}"/>
         </GridView>
     </ListView.View>
</ListView>

Then, MeasurementModel.cs

public class MeasurementModel
    {
        public string Name { get; set; }
        public double Data { get; set; }
        public string Unit { get; set; }

        public double Roll { get; set; }
        public double Pitch { get; set; }
        public double Yaw { get; set; }
    }

tableServer.cs

public class tableServer
    {

        public dynamic getMeasurementsOri() 
        {
            var json = new WebClient().DownloadString("http://ip/file.php");

            return JArray.Parse(json);
        }

    }

TableViewModel.cs

public class TableViewModel : INotifyPropertyChanged
    {
        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                if (_name != value)
                {
                    _name = value;
                    OnPropertyChanged("Name");
                }
            }
        }

        private double _roll;
        public string Roll
        {
            get
            {
                return _roll.ToString("0.0####", CultureInfo.InvariantCulture);
            }
            set
            {
                if (Double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out double tmp) && _roll != tmp)
                {
                    _roll = tmp;
                    OnPropertyChanged("Data");
                }
            }
        }

        private double _pitch;
        public string Pitch
        {
            get
            {
                return _pitch.ToString("0.0####", CultureInfo.InvariantCulture);
            }
            set
            {
                if (Double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out double tmp) && _pitch != tmp)
                {
                    _pitch = tmp;
                    OnPropertyChanged("Data");
                }
            }
        }

        private double _yaw;
        public string Yaw
        {

            get
            {
                return _yaw.ToString("0.0####", CultureInfo.InvariantCulture);
            }
            set
            {
                if (Double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out double tmp) && _yaw != tmp)
                {
                    _yaw = tmp;
                    OnPropertyChanged("Data");
                }
            }
        }

        private double _data;
        public string Data
        {
            get
            {
                return _data.ToString("0.0####", CultureInfo.InvariantCulture);
            }
            set
            {
                if (Double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out double tmp) && _data != tmp)
                {
                    _data = tmp;
                    OnPropertyChanged("Data");
                }
            }
        }

        private string _unit;
        public string Unit
        {
            get
            {
                return _unit;
            }
            set
            {
                if (_unit != value)
                {
                    _unit = value;
                    OnPropertyChanged("Unit");
                }
            }
        }

        public TableViewModel(MeasurementModel model)
        {
            UpdateWithModel(model);
        }

        public void UpdateWithModel(MeasurementModel model)
        {
            _name = model.Name;
            OnPropertyChanged("Name");
            _data = model.Data;
            OnPropertyChanged("Data");
            _unit = model.Unit;
            OnPropertyChanged("Unit");
            _roll = model.Roll;
            OnPropertyChanged("Roll");
            _pitch = model.Pitch;
            OnPropertyChanged("Pitch");
            _yaw = model.Yaw;
            OnPropertyChanged("Yaw");
        }

        #region PropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        /**
         * @brief Simple function to trigger event handler
         * @params propertyName Name of ViewModel property as string
         */
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }

All until this point works correctly. ListView is generating with proper columns. Now this part is making me confused:

MainViewModel.cs

void RefreshHandlerOri()
        {
            // Read data from server in JSON array format
            dynamic measurementsJsonArray = iotTableOri.getMeasurementsOri();

            // Convert generic JSON array container to list of specific type
            dynamic measurementsListA = measurementsJsonArray.ToObject<List<MeasurementModel>>();


            // Add new elements to collection
            if (OriMeasurements.Count < measurementsListA[0].Count)
            {
                foreach (var m in measurementsListA[0])
                    OriMeasurements.Add(new TableViewModel(m));

            }
            // Update existing elements in collection
            else
            {
                for (int i = 0; i < OriMeasurements.Count; i++)
                {
                    OriMeasurements[i].UpdateWithModel(measurementsListA[0]);
                }
            }

        }

How can i extract those values from the json shown, to my listview. I have tried many options but mostly I get an error Error reading double, Unexpected token: StartObject.Path '[0].data', line 1, position 11.' Any help would be appreciated.

jnkvcu
  • 11
  • 3
  • Your Model does not match the json. `MeasurementModel` should have a Property `Data` which would be an Object that has the Properties `Roll`, `Pitch` and `Yaw`. – Fildor Jun 17 '20 at 11:13
  • As soon as i posted this question, I saw that mistake. Now I am trying to change my code accordingly, but i am not really sure how to do it. – jnkvcu Jun 17 '20 at 11:50
  • Paste JSON as Code generated this: public class Rootobject { public Class1[] Property1 { get; set; } } public class Class1 { public Data data { get; set; } public string name { get; set; } public string unit { get; set; } } public class Data { public float yaw { get; set; } public float roll { get; set; } public float pitch { get; set; } } – Jan Jun 17 '20 at 12:46
  • Alright so if I place public class Data { public double Roll {get; set;} etc. } and public Data data {get; set; } in MeasurementModel.cs, I have to make function public Data data in my TableViewModel.cs. How can I make that function in my ViewModel folder, when the function is defined in Model folder and what do I get and set? – jnkvcu Jun 17 '20 at 13:07

0 Answers0