mvc, json, c#

ITsolutionWizard
ITsolutionWizard used Ask the Experts™
on
I have the following json string. how  to read it in c# FOR  LOOP?

{Employee:[{"principalfirstname":"xxx","principallastname":"xxx","principalmiddlename":"xxx","principalnameonbond":"xxx","principaldba":"xxx","principalentity":"USA","principalyearinbusiness":"xxx","principaltaxid":"","principaladdress":"xx","principalcity":"xxx","principalstate":"TN","principalzip":"111","principalcounty":"","principalphoneno":"xxx","principalemail":"","principalfaxno":"","vin":"","licenseplatenumber":"","driverlicensenumber":"","door":"","bondstate":"","bondamount":"","premium":"","bondterm":"","creditcardfullname":"","creditcardnumber":"55555555533331111","creditcardchargeamount":"","creditcardexpdatemm":"","creditcardexpdateyyyy":"","creditcardcvc":"","creditcardaddress":"","creditcardcity":"","creditcardstate":"","creditcardzip":""}]}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Hello!

Try to use this code:
dynamic input = JsonConvert.DeserializeObject(inputJsonString);

Open in new window


Look this for more information:
https://www.newtonsoft.com/json/help/html/Overload_Newtonsoft_Json_JsonConvert_DeserializeObject.htm

Commented:
Please do understand that the experts here are volunteers.

As for your issue:
using Newtonsoft.Json;
using System;

namespace EE_Q29158005
{
    class Program
    {
        static string data = "{Employee:[{'principalfirstname':'xxx','principallastname':'xxx','principalmiddlename':'xxx','principalnameonbond':'xxx','principaldba':'xxx','principalentity':'USA','principalyearinbusiness':'xxx','principaltaxid':'','principaladdress':'xx','principalcity':'xxx','principalstate':'TN','principalzip':'111','principalcounty':'','principalphoneno':'xxx','principalemail':'','principalfaxno':'','vin':'','licenseplatenumber':'','driverlicensenumber':'','door':'','bondstate':'','bondamount':'','premium':'','bondterm':'','creditcardfullname':'','creditcardnumber':'55555555533331111','creditcardchargeamount':'','creditcardexpdatemm':'','creditcardexpdateyyyy':'','creditcardcvc':'','creditcardaddress':'','creditcardcity':'','creditcardstate':'','creditcardzip':''}]}";
        static void Main(string[] args)
        {
            var response = JsonConvert.DeserializeObject<dynamic>(data);
            foreach (var value in response.Employee)
            {
                foreach (var pair in value)
                {
                    Console.WriteLine(pair);
                }
            }
            Console.ReadLine();
        }
    }
}

Open in new window

Produces the following results -Capture.PNG-saige-
Jose Gabriel Ortega CastroCEO Faru Bonon IT /Top Rated Freelancer on Upwork / Photographer
Awarded 2018
Distinguished Expert 2018

Commented:
Btw that JSON is not well-formatted, you should have something wrong on the format of that JSON

Bad formatting of Json

Author

Commented:
it_saige: Your codes won't work to me. and I have  "" for each value. Your example string is different from mine.

Thanks

{Employee:[{"principalfirstname":"xxx","principallastname":"xxx","principalmiddlename":"xxx","principalnameonbond":"xxx","principaldba":"xxx","principalentity":"USA","principalyearinbusiness":"xxx","principaltaxid":"","principaladdress":"xx","principalcity":"xxx","principalstate":"TN","principalzip":"111","principalcounty":"","principalphoneno":"xxx","principalemail":"","principalfaxno":"","vin":"","licenseplatenumber":"","driverlicensenumber":"","door":"","bondstate":"","bondamount":"","premium":"","bondterm":"","creditcardfullname":"","creditcardnumber":"55555555533331111","creditcardchargeamount":"","creditcardexpdatemm":"","creditcardexpdateyyyy":"","creditcardcvc":"","creditcardaddress":"","creditcardcity":"","creditcardstate":"","creditcardzip":""}]}
Commented:
This is the exact same thing as above, except instead of using single ticks, it is escaping the double quotes:
using Newtonsoft.Json;
using System;

namespace EE_Q29158005
{
    class Program
    {
        static string data = "{Employee:[{\"principalfirstname\":\"xxx\",\"principallastname\":\"xxx\",\"principalmiddlename\":\"xxx\",\"principalnameonbond\":\"xxx\",\"principaldba\":\"xxx\",\"principalentity\":\"USA\",\"principalyearinbusiness\":\"xxx\",\"principaltaxid\":\"\",\"principaladdress\":\"xx\",\"principalcity\":\"xxx\",\"principalstate\":\"TN\",\"principalzip\":\"111\",\"principalcounty\":\"\",\"principalphoneno\":\"xxx\",\"principalemail\":\"\",\"principalfaxno\":\"\",\"vin\":\"\",\"licenseplatenumber\":\"\",\"driverlicensenumber\":\"\",\"door\":\"\",\"bondstate\":\"\",\"bondamount\":\"\",\"premium\":\"\",\"bondterm\":\"\",\"creditcardfullname\":\"\",\"creditcardnumber\":\"55555555533331111\",\"creditcardchargeamount\":\"\",\"creditcardexpdatemm\":\"\",\"creditcardexpdateyyyy\":\"\",\"creditcardcvc\":\"\",\"creditcardaddress\":\"\",\"creditcardcity\":\"\",\"creditcardstate\":\"\",\"creditcardzip\":\"\"}]}";
        static void Main(string[] args)
        {
            var response = JsonConvert.DeserializeObject<dynamic>(data);
            foreach (var value in response.Employee)
            {
                foreach (var pair in value)
                {
                    Console.WriteLine(pair);
                }
            }
            Console.ReadLine();
        }
    }
}

Open in new window

And produces the exact same output -Capture.PNG-saige-

Author

Commented:
I do not  get  it. can you tell me what you want me to do in javascript for modifying the following string.

{Employee:[{"principalfirstname":"xxx","principallastname":"xxx","principalmiddlename":"xxx","principalnameonbond":"xxx","principaldba":"xxx","principalentity":"USA","principalyearinbusiness":"xxx","principaltaxid":"","principaladdress":"xx","principalcity":"xxx","principalstate":"TN","principalzip":"111","principalcounty":"","principalphoneno":"xxx","principalemail":"","principalfaxno":"","vin":"","licenseplatenumber":"","driverlicensenumber":"","door":"","bondstate":"","bondamount":"","premium":"","bondterm":"","creditcardfullname":"","creditcardnumber":"55555555533331111","creditcardchargeamount":"","creditcardexpdatemm":"","creditcardexpdateyyyy":"","creditcardcvc":"","creditcardaddress":"","creditcardcity":"","creditcardstate":"","creditcardzip":""}]}

Author

Commented:
this is my original code to generate  the  string

function Submit(domain) {
    //alert("Thank You");
    var x = $('#formdiv *').serializeArray();
    var data = {};
    $.each(x, function (i, field) {
        data[field.name] = field.value;
    });      

    var json_new = "{Employee:[" + json.replace(/\"/g,"") + "]}";

    alert(json_new);
    $.ajax({
        url: domain + "/Api/ReceiveJson",
        type: "POST",
        data: {
            'message': json_new
        },
        dataType: 'json',
        success: function (data) {
            alert(data.Code);
            //window.open(domainHost + data.filename, openBlank);
        },
        error: function (msg) { alert(msg); }
    });
    return false;
}

Open in new window

Commented:
You don't have to do anything in Javascript, when your C# application receives a JSON object, it is presented as I have shown as a serialized string.

Normally this presentation takes the form of:
"{\"property1\":\"value1\"}"

Open in new window

But when you use many JSON visualizers, the escape characters are stripped so all you see is:
"{"property1":"value1"}"

Open in new window

-saige-

Author

Commented:
with the us code , it does not generate the string you post that means it won’t work

Author

Commented:
???

Commented:
Can you provide the method code on the C# side you are using to deserialize the json?

Could you also provide a screen shot of where you are seeing your json as formatted above?

Thanks,

-saige-

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial