-1

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):

        //call openapi.ro
        string CompanyCUI = "22052442";
        // Create a new 'Uri' object with the specified Company ID string.
        Uri myUri = new Uri("https://api.openapi.ro/api/companies/"+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

net4u
  • 43
  • 9
  • Your json does not validate with jsonlint.com. You should have your fields in double-quotes as well like this "ultima_prelucrare": "2013-11-25", "ultima_declaratie": "2013-11-25",.... – smoore4 Sep 05 '16 at 06:34
  • Show the model for `CustomerModels` –  Sep 05 '16 at 06:36
  • Now is like: public class CustomerModels { // public string ultima_prelucrare { get; set; } public string ultima_declaratie { get; set; } public List tva_la_incasare { get; set; } public string tva { get; set; } public string telefon { get; set; } public string accize { get; set; } } – net4u Sep 05 '16 at 07:15
  • You need to edit the question (and to show what it was, not what worked!) Clearly it was not `IEnumerable` –  Sep 05 '16 at 07:25

1 Answers1

0

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; }
Halis S.
  • 446
  • 2
  • 11
  • thank you, now is ok. In terms of "human reading" I have now idea what kind of info can be there, since I am not VAT payer. I will just check with the ID of a company that I know that is VAT payer to see for curiosity what I may have there. – net4u Sep 05 '16 at 06:47