简体   繁体   中英

How to retrieve the values in a multi dimensional array using JSON?

I have created a multi dimensional array.. which stores the table rows values from database..I need to store the array elements into json format,and should display the array elements seperately(ie,row wise as in the data base table). my code is here..but it displays all the data in json format like this. :

{"question":"6"}{"question":"mother?"}{"question":"fghh"}{"question":"bbb"}{"question":"qwe"}{"question":"wer"}{"question":"5"}{"question":"colg?"}{"question":"sdf"}{"question":"scf"}{"question":"aaa"}{"question":"dfdf"}

my code is here :

  SqlDataAdapter da = new SqlDataAdapter("select question_num,q_text,answer1_text,answer2_text,answer3_text,answer4_text from question_bank_details  where question_num  in (select top 2 question_num  from question_bank_details order by newid())", con5);
    DataTable dt = new DataTable();

    da.Fill(dt);
 int i; int j;

    string[,] array_questions = new string[dt.Rows.Count, dt.Columns.Count];

    for (i = 0; i < dt.Rows.Count; i++)
    {
        for (j = 0; j < dt.Columns.Count; j++)
        {
            array_questions[i, j] = dt.Rows[i][j].ToString();
            //Response.Write(array_questions[i, j]);
        }

    }
    foreach (string question_row in array_questions)
    {

        var ques = new { question = question_row };
        JavaScriptSerializer js = new JavaScriptSerializer();
        // string json = js.Serialize(question_row);
        string json = js.Serialize(ques);

        Response.ContentType = "application/json";
        Response.Write(json);

    }


}

how can i display it row wise as in the database table ? please help ? I need the output like this :

{"question" : "6" "mother?" "bbb" "fghhh" "bbb" "qwe" "wer"}{"question" : "5" "colg" "sdf" "scf" "aaa" "ddd" "hjk"}

If you are wanting a JSON array of objects with named properties, try the following:

var rows = dt.Rows
    .Cast<DataRow>()
    .Select(row => new {
        ID = row[0],
        Question = row[1],
        Answer1 = row[2],
        Answer2 = row[3],
        Answer3 = row[4],
        Answer4 = row[5],
    })
    .ToArray();
string json = js.Serialize(rows);

If instead you want an array of arrays as JSON, try this:

dt.Rows
    .Cast<DataRow>()
    .Select(row => new string[] {
        row[0].ToString(),
        row[1].ToString(),
        row[2].ToString(),
        row[3].ToString(),
        row[4].ToString(),
        row[5].ToString(),
    })
    .ToArray();
string json = js.Serialize(rows);

If you want something else, please update your question accordingly.

This link might help you. It shows how you can handle multi dimensional array.

http://www.mikesdotnetting.com/Article/96/Handling-JSON-Arrays-returned-from-ASP.NET-Web-Services-with-jQuery

Your serializing turns the data into JSON . What you are trying to do is not JSON.

According to the information you gave you are trying to do something which isn't standard - you'll probably need to code it yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM