Printing json data on a webform properly

I’m using N1QL query to get data from my couchbase bucket and print it on a web form as JSON with asp.NET. The point is, my page looks like this :

my page

but I need it to look like this :

expectation

also, as you can see, I got an error on my page.

and the little part of code that I get and print data is following :

var myBucket = myCluster.OpenBucket();
const string query = "SELECT forumID, forumTitle, forumDesc FROM default";
var data = myBucket.Query<dynamic>(query);
Response.Clear();
Response.ContentType = "application/json; chartset=utf-8";
foreach (var item in data.Rows)
{
 Response.Write(item);
}     

Response.End();

I also tried to put comma between the rows, and square brackets with using:

Response.Write("["+item+"],");

But it just fixed the error, the appearance was still the same. Also when I do it there appears a comma at the end of the last item too, which I don’t want.

When I execute a query on the couchbase console the result appears to be in json format but I don’t know why it becomes like that on webform and how to fix it.

Anyone who can help me with that? I don’t think it requires any couchbase or N1QL knowledge but I might be missing something. I need help with that json error either.

Regards, and thanks!

Your correct here, this all about how you are displaying your data and formatting it. You can try setting the Pretty flag on a QueryRequest object, but it’s probably not the effect you desire:

 ...     
 var myBucket = myCluster.OpenBucket();
 const string query = "SELECT forumID, forumTitle, forumDesc FROM default";
 var queryRequest = new QueryRequest(query).Pretty(true);
 var data = myBucket.Query<dynamic>(queryRequest);
 ...

If that doesn’t help, I suspect there is a some JavaScript plugin available that could help you do this without having to write one yourself. In this case, your probably better off posting this on Stackoverflow and tagging it with HTML5, JavaScript, CSS, etc…front-end web technologies.

-Jeff

1 Like

Hello @jmorris ,

I tried to serialize the json I got from couchbase and took every column with LINQ, that solved the problem. Maybe it’s not the efficient way but I guess because of the way couchbase holds data, I have to do it one by one. Because every document in my bucket holds only one item. Thats what I thought.

Thanks anyways and I’m still open for any improvements. Here’s my new code :

var myBucket = myCluster.OpenBucket();

const string query = "SELECT forumID, forumTitle, forumDesc, sort, topics, total FROM default";
var data = myBucket.Query<dynamic>(query);

JavaScriptSerializer serializer = new JavaScriptSerializer();

var jsonData = serializer.Serialize(data.Rows.Select(q => new forumVeri { forumDesc = q.forumDesc, forumID = q.forumID, forumTitle = q.forumTitle , sort = q.sort , topics = q.topics , total = q.total}));

Response.Clear();
Response.ContentType = "application/json; chartset=utf-8";

Response.Write(jsonData);

Response.End();
1 Like

@HandeBc

You might try using Newstonsoft.Json with JsonConvert.SerializeObject instead of JavaScriptSerializer. My understanding is that it’s more efficient. Since you’re using the Couchbase SDK, this library should already be installed in your project.

Thanks,
Brant

1 Like

hello @jmorris ,

I need help with creating views for my bucket and use them to query. Turns out I shouldn’t use LINQ. How can I create views and take data from them, then print them as JSON properly? I can’t find the exact way on the forum and I’m so new at this.

Thank you.