Click here to Skip to main content
14,878,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am using a native Microsoft library instead of Newtonsoft (I cannot use Newtonsoft as the application we use does not support third-paty libraries) to parse a json response and write to database. I am stuck with how to parse json.

The error that I get is
Quote:
foreach statement cannot operate on variables of type RootObject because RootObject does not contain a public definition for GetEnumerator


Could you please guide me.

What I have tried:

public class RootObject
{
    public string Id { get; set; }
    public string Code { get; set; }
    public string Description { get; set; }
}	
	
                string cities;

                cities = getCities();

                JavaScriptSerializer oJS = new JavaScriptSerializer();
                RootObject oRootObject = new RootObject();
                oRootObject = oJS.Deserialize<RootObject>(cities);

                string connectionString = @"Data Source=server\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=SSPI;";							
                string sqlTable = "test";

            foreach (var city in oRootObject) //foreach statement cannot operate on variables of type RootObject because RootObject does not contain a public definition for GetEnumerator
			{
				using (SqlConnection sc = new SqlConnection(connectionString))
                {
                    sc.Open();
                    using (SqlBulkCopy sbc = new SqlBulkCopy(sc))
                    {
                        sbc.DestinationTableName = sqlTable;
                        sbc.BatchSize = dt.Rows.Count;
                        sbc.BulkCopyTimeout = 0;
                        sbc.ColumnMappings.Add("Id", "Id");
                        sbc.ColumnMappings.Add("Code", "Code");
                        sbc.ColumnMappings.Add("Description", "Description");

                        sbc.WriteToServer(dt);
                    }
                }	
			}

JSON

[
{
    "Id": "1",
    "Code": "Man",
    "Description": "Manchester"
},
{
    "Id": "2",
    "Code": "Lon",
    "Description": "London"
}
]
Posted
Updated 21-Dec-19 13:50pm
v2
Comments
Member 12586110 21-Dec-19 15:06pm
   
Thanks all for your response. I am still unable to covert json string as a custom .net object. I will keep working on it and keep you posted as I explore more.

Regards

You cannot use for each because oRootObject is not a list, it's only a single object.

if the response is a list of objects you can try something like
...
cities = getCities();
JavaScriptSerializer oJS = new JavaScriptSerializer();
var rootObjects = oJS.Deserialize<List<RootObject>>(cities);
...
foreach (var city in rootObjects)
...
   
v2
   
Well no - it doesn't.
And there isn't anything you can do about that because it also contains no collection of items, so there isn't anything you could iterate over, so nothing you could write an enumerator to work with, and thus nothing that foreach can process.

Start by taking your JSON and feeding it to a class generator: json2csharp - generate c# classes from json[^] is the one I use.
Then look at the classes and see what you should be working with. If you have to process JSON that changes structure, you can then work back from those with your JSON to produce code that stands a vague chance of working.
   
I've written an article to answer questions like this one: Working with JSON in C# & VB[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900