Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

mvc, json, c#

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

Avatar of Misha
Misha
Flag of Russian Federation image

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
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 -User generated image-saige-
Btw that JSON is not well-formatted, you should have something wrong on the format of that JSON

User generated image
Avatar of ITsolutionWizard

ASKER

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":""}]}
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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":""}]}
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

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-
with the us code , it does not generate the string you post that means it won’t work
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-