1

I an using json.net to serialize my DataTable. I pass this json string (the result) to the view to render it in an unorder list. But json format is messed up in the result

DataAccess

public static string getClinic()
{
    string sproc = "getClinic";
    return callProcedure(sproc);
}

public static string callProcedure(string sproc)
{
    DataTable ds = null;

    try
    {
        using (SqlConnection sqlConn = new SqlConnection(sqlConnString))
        {
            SqlCommand cmd = new SqlCommand(sproc, sqlConn);
            cmd.CommandType = CommandType.StoredProcedure;
            sqlConn.Open();

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);

            ds = new DataTable();
            adapter.Fill(ds);

            string json = JsonConvert.SerializeObject(ds);
            return json;
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

controller

public ActionResult Index()
{

    QueryModel qModel = new QueryModel {
        tblClinic = new Clinic(),

    };
    return View("Index", qModel);
}

View

<ul>
    @foreach (var p in Model.tblClinic.clinic){
        <li>@p</li>
    }
</ul>

But my data is messed up like this:

[
{
"
c
o
d
e
"
:
0
,
"
n
a
m
e
"
:
"
A"
}
,
{
...
}

I expect to be:

code: 0, name: A
code: 1, name: B

...

Do I need to deserialize the json object first before looping?

More information: my model

public class QueryModel
{
    public Clinic tblClinic { get; set; }

}

my domain

public class Clinic
{
    public string clinic { get; set; }
    public Clinic() {
        this.clinic = Data.getClinic();       
    }
}
3
  • 1
    We don't need to see all your different layers of code, just the part that is applicable to the question at hand. Input, output, and a minimally complete/testable example are what you need. Commented Mar 10, 2014 at 18:45
  • You write your json string's each char line by line, @foreach (var p in Model.tblClinic.clinic){ <li>@p</li> } Commented Mar 10, 2014 at 18:46
  • 2
    It's doing exactly what you're telling it to. The <li> adds a line break after each character you are iterating though. The foreach has no idea you want to deserialize a JSON string. Commented Mar 10, 2014 at 18:48

1 Answer 1

1

Do I need to deserialize the json object first before looping?

Yes, obviously. You need to deserialize the json object first before looping. Otherwise, looping through a string will only produce a char at each step.

Deserialization in the class may be something like:

public class RootObject // you have to set the class name
{
    public int code { get; set; }
    public string name { get; set; }
}

Then in the View, you can loop like below:

<ul>
    @foreach (RootObject p in JsonConvert.DeserializeObject<List<RootObject>>(Model.tblClinic.clinic))
    {
        <li>code: @p.code, name: @p.name</li>
    }
</ul>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.