Click here to Skip to main content
15,898,937 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I was developing a web application which retrieves json data as output and now i want it to be shown in a datatable.When i use newtosoft json its saying
""An exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll but was not handled in user code.
I was new in web api. my json is containing lots of data even in arrays... please help

What I have tried:

public void Button1_Click(object sender, EventArgs e)
        {
           
            string myDynamicJSON = jsontb.Text;
            

            //Using dynamic keyword with JsonConvert.DeserializeObject, here you need to import Newtonsoft.Json  
            dynamic myObject = JsonConvert.DeserializeObject(myDynamicJSON);

            //Binding gridview from dynamic object   
            GridView1.DataSource = myObject;
            GridView1.DataBind();

            //Using DataTable with JsonConvert.DeserializeObject, here you need to import using System.Data;  
            DataTable myObjectDT = JsonConvert.DeserializeObject<datatable>(myDynamicJSON);

            //Binding gridview from dynamic object   
            GridView1.DataSource = myObjectDT;
            GridView1.DataBind();
        }
Posted
Updated 30-May-18 8:44am
v2
Comments
F-ES Sitecore 30-May-18 7:41am    
We can't suggest what might be wrong without knowing what the json you are trying to deserialise is. I suggest you google the error message and go through some scenarios where this can happen and see if any apply to you.

Quote:
Additional text encountered after finished reading JSON content: ; . Path '', line 1, position 46."

If you read carefully the error message; it tells you that your json file is not a json file after the position indicated.
Only thing to do with your code is to protect it against such problem with a try/catch structure.
Otherwise, what is interesting is a dump of beginning of your json file.
 
Share this answer
 
your code do not have enough data to give a proper solution.

but in my opinion your problem is with the DeserializeObject line

DataTable myObjectDT = JsonConvert.DeserializeObject<datatable>(myDynamicJSON);

the data you deserializing may not be same as datatable. check if the object type matches for myDynamicJSON and datatable. One common mistake is that people try to deserialize collection of object to a single object. please check if it is true in your case.
 
Share this answer
 

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