C
C#•10mo ago
Christian Dale

Extracting JSON Element into a ComboBox Data source

I have a Json file that i what to Extract one element of it into a Data Source for a Combo Box The Element I can to extract is the ModelNumber I am using NewtonSoft.Json Package for the JSON Serialize
5 Replies
Hazel 🌊💃
public class MyDataModelName {
public CommonElementCollection CommonElements { get; set; }
public Model[] Models { get; set; }
}
public class Model {
public string ModelNumber { get; set; }
...
}

...
var data = Deserialize<MyDataModelName>(jsonString);
foreach (var model in data.Models)
comboBox.Items.Add(model.ModelNumber);
public class MyDataModelName {
public CommonElementCollection CommonElements { get; set; }
public Model[] Models { get; set; }
}
public class Model {
public string ModelNumber { get; set; }
...
}

...
var data = Deserialize<MyDataModelName>(jsonString);
foreach (var model in data.Models)
comboBox.Items.Add(model.ModelNumber);
Something like that.
Christian Dale
Christian DaleOP•10mo ago
when i try that way it is givng me this error. 'Unexpected character encountered while parsing number: . Path '', line 1, position 1.'
Hazel 🌊💃
$code
MODiX
MODiX•10mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Christian Dale
Christian DaleOP•10mo ago
namespace Hik_Config
{
public partial class Form1 : Form
{
private NvrSettings _nvrSettings;
public Form1(NvrSettings nvrSettings)
{
var data = JsonConvert.DeserializeObject<Model>(@".\nvrModels.json");
foreach (var model in data.Models) {
NVRModel.Items.Add(model.ModelNumber);
}
InitializeComponent();
_nvrSettings = nvrSettings;
JSONLOAD();

}

private void Loud_Btn_Click(object sender, EventArgs e)
{
JSONLOAD();
}

public void JSONLOAD()
{
IpAddressTextBox.Text = _nvrSettings.IpAddress;
UserNameTextBox.Text = _nvrSettings.UserName;
PasswordTextBox.Text = _nvrSettings.Password;
NVRModel.Text= _nvrSettings.Model;
}

private void Save_Btn_Click(object sender, EventArgs e)
{
//string json_txt = File.ReadAllText(@".\Config.json");
//NvrSettings NVR = JsonConvert.DeserializeObject<NvrSettings>(json_txt);
if (IsAddressValid(IpAddressTextBox.Text) == true)
{
_nvrSettings.IpAddress = IpAddressTextBox.Text;
_nvrSettings.UserName = UserNameTextBox.Text;
_nvrSettings.Password = PasswordTextBox.Text;
_nvrSettings.Model = NVRModel.Text;

// Wrap NvrSettings in a RootObject
RootObject rootObject = new RootObject
{
NvrSettings = _nvrSettings
};

File.WriteAllText(@".\Config.json", JsonConvert.SerializeObject(rootObject, Formatting.Indented));
MessageBox.Show("NVR Loader Config Saved Successfully", "SAVED");
Close();
Application.Exit();

}
else
{
MessageBox.Show("INVALID IP ADDRESS", "INVALID IP ADDRESS");
}
}
public bool IsAddressValid(string addrString)
// Check IP is Valid
{
string pattern = @"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
Regex regex = new Regex(pattern);
return regex.IsMatch(addrString);

//IPAddress address;
//return IPAddress.TryParse(addrString, out address) && address.ToString() == addrString;
}
}

}
namespace Hik_Config
{
public partial class Form1 : Form
{
private NvrSettings _nvrSettings;
public Form1(NvrSettings nvrSettings)
{
var data = JsonConvert.DeserializeObject<Model>(@".\nvrModels.json");
foreach (var model in data.Models) {
NVRModel.Items.Add(model.ModelNumber);
}
InitializeComponent();
_nvrSettings = nvrSettings;
JSONLOAD();

}

private void Loud_Btn_Click(object sender, EventArgs e)
{
JSONLOAD();
}

public void JSONLOAD()
{
IpAddressTextBox.Text = _nvrSettings.IpAddress;
UserNameTextBox.Text = _nvrSettings.UserName;
PasswordTextBox.Text = _nvrSettings.Password;
NVRModel.Text= _nvrSettings.Model;
}

private void Save_Btn_Click(object sender, EventArgs e)
{
//string json_txt = File.ReadAllText(@".\Config.json");
//NvrSettings NVR = JsonConvert.DeserializeObject<NvrSettings>(json_txt);
if (IsAddressValid(IpAddressTextBox.Text) == true)
{
_nvrSettings.IpAddress = IpAddressTextBox.Text;
_nvrSettings.UserName = UserNameTextBox.Text;
_nvrSettings.Password = PasswordTextBox.Text;
_nvrSettings.Model = NVRModel.Text;

// Wrap NvrSettings in a RootObject
RootObject rootObject = new RootObject
{
NvrSettings = _nvrSettings
};

File.WriteAllText(@".\Config.json", JsonConvert.SerializeObject(rootObject, Formatting.Indented));
MessageBox.Show("NVR Loader Config Saved Successfully", "SAVED");
Close();
Application.Exit();

}
else
{
MessageBox.Show("INVALID IP ADDRESS", "INVALID IP ADDRESS");
}
}
public bool IsAddressValid(string addrString)
// Check IP is Valid
{
string pattern = @"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
Regex regex = new Regex(pattern);
return regex.IsMatch(addrString);

//IPAddress address;
//return IPAddress.TryParse(addrString, out address) && address.ToString() == addrString;
}
}

}
From the JSON the only thing i need to pull for this are the ModelNumbers. I have found the Issue. 2 Fold i forgor to pass the Fileread into the Variable and also the ComboBox Add needed to be in the Form Design Section.
Want results from more Discord servers?
Add your server