net4u net4u - 1 year ago 123
ASP.NET (C#) Question

Trouble with json in mvc

In an MVC application I retrieve a json string from a given server.
Fist of all I retrieve for testing purpose the json with Chrome and I get this

ultima_prelucrare: "2013-11-25",
ultima_declaratie: "2013-11-25",
tva_la_incasare: [ ],
tva: null,
telefon: "0745040840",
stare: "INREGISTRAT din data 04 Iulie 2007",
radiata: false,
numar_reg_com: "J40/12836/2007",
meta: {
updated_at: "2016-08-30T19:05:29.922418",
last_changed_at: null
judet: "Municipiul București",
impozit_profit: null,
impozit_micro: "2011-01-01",
fax: null,
denumire: "Infosystems4u S.R.L.",
cod_postal: "61954",
cif: "22052442",
adresa: "Aleea Baiut, 9a, Bucuresti",
act_autorizare: null,
accize: null

and is OK.
Now back to my app. I already have a model that describe this structure. I have a view for adding a customer in my application with this fields.
In my scenario I want that after providing a value for "cif" field, and then to make a web request against the given server and retrieve the above json .
The json is retrieved but when I try to deserialize with newtonsoft it raise an exception about "[" character. Here I noticed that [ is not enclosed between quote character but is like this how it comes from server.
On very short my code is like (now the code is simply inside the default action in controller, but I can address this later):

string CompanyCUI = "22052442";
// Create a new 'Uri' object with the specified Company ID string.
Uri myUri = new Uri(""+CompanyCUI+".json");
// Create a new request to the above mentioned URL.
WebRequest myWebRequest = WebRequest.Create(myUri);
//Add the required header to request
myWebRequest.Headers.Add("x-api-key", "8P4RP_kwn71Nt8VG7boFmQb_7NsihyQxT_x7JGcGQkvPdXZH2Q");
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse = myWebRequest.GetResponse();
// Read the response into a stream
var dataStream = myWebResponse.GetResponseStream();
var reader = new StreamReader(dataStream);
var jsonResultString = reader.ReadToEnd();
// Deserialize
var CompanyInfoData = Newtonsoft.Json.JsonConvert.DeserializeObject<CustomerModels>(jsonResultString);
//Feed the model with retrieved data
//Save all

Now supposing that I find what can be wrong with json content, and eventualy how to go further, next step is to pre fill the editbox controls in my form with retrieved values before the user hit the Save button of the form. What bothers me is that I need two buttons inside my form, one for invoking the web request and fill with desired values, and second the save button, and I really don't know how to deal with two buttons inside a single form.

Thank you in advance for any hint

Answer Source

The JSON provided is syntatically valid. When declaring CustomerModels object, make sure that tva_la_incasare attribute is declared as array or list like below:

public List<object> tva_la_incasare { get; set; }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download