I need to send data from my game server to the client through json and getting a large sum of data from the database and sending it creates an issue
Currently I am creating multiple list in the call to the database to get the data and now I am stuck because I have the data but in multiple list and I can't figure out how to return the data.
I feel like I should be converting it to JSON here and then returning the json string but its all just really confusing
public static void GetLobbyList() {
string query = "SELECT * FROM que";
MySqlCommand cmd = new MySqlCommand(query, MySQL.mySQLSettings.connection);
MySqlDataReader reader = cmd.ExecuteReader();
List<int> ids = new List<int>();
List<string> uids = new List<string>();
List<int> bets = new List<int>();
List<string> games = new List<string>();
while (reader.Read()) {
ids.Add((int)reader["id"]);
uids.Add((string)reader["uid"]);
bets.Add((int)reader["bet"]);
games.Add((string)reader["game"]);
}
reader.Close();
}
So here I am reading the info from the database and for lack of experience I am adding each data point to a list(which is really ugly and I know there must be a better way)
So essentially I Grab the data->Parse to json->Send string to client
If It is possible assuming the data returned is from a table with
id | uid | bet | game
I would Like to return an array in json that looks like
{
{
"id" : 1,
"uid" : "erjfh4982y9hf",
"bet" : 3,
"game" : "Snake"
}
{
"id" : 2,
"uid" : "gsegt34t",
"bet" : 2,
"game" : "Snake"
}
}
Im not too familiar with json and how it works but I do know that it is the only way to send large packets of data from my server to my client because everything has to be converted to bytes before it can be sent and my framework does not support converting list to bytes
To achieve what you are aiming for, you should create a class to hold the data you have retrieved from the database. In my case I called it GameObject and it's defined as follows.
public class GameObject
{
public int Id { get; set; }
public string Uid { get; set; }
public int Bet { get; set; }
public string Game { get; set; }
}
After retrieving the information from the database, you'll need to run code similar to the following.
var items = new List<GameObject>();
while (reader.Read())
{
items.Add(new GameObject
{
Id = (int)reader["id"],
Uid = (string)reader["uid"],
Bet = (int)reader["bet"],
Game = (string)reader["game"]
});
}
// Return the jsonPacket which will contain all the items in json format.
var jsonPacket = JsonConvert.SerializeObject(items);
In order for this to work, you'll need to reference the Newtonsoft.Json library from nuget. Open your package manager console and type the following command: Install-Package Newtonsoft.Json and it will set it up for you. At the top of our code you'll need to have using Newtonsoft.Json; to be able to use the classes inside the library to serialise to Json. On the receiving end you can pass the string to JsonConvert.DeserializeObject<T>() and you'll get back your list of objects.
You could use a list of objects and anonymous types. There's also a JSON serializer, System.Web.Script.Serialization.JavaScriptSerializer included if you add the System.Web.Extensions assembly to your references.
List<object> list = new List<object>();
while (reader.Read()) {
list.Add(new { id = (int)reader["id"],
uid = (string)reader["uid"],
bet = (int)reader["bet"],
game = (string)reader["game"], });
}
string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(list);
Related
I asked a question a couple of days ago to collect data from MongoDB as a tree.
MongoDB create an array within an array
I am a newbie to MongoDB, but have used JSON quite substantially. I thought using a MongoDB to store my JSON would be a great benefit, but I am just experiencing immense frustration.
I am using .NET 4.5.2
I have tried a number of ways to return the output from my aggregate query to my page.
public JsonResult GetFolders()
{
IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("DataStore");
PipelineDefinition<BsonDocument, BsonDocument> treeDocs = new BsonDocument[]
{
// my query, which is a series of new BsonDocument
}
var documentGroup = collection.Aggregate(treeDocs).ToList();
// Here, I have tried to add it to a JsonResult Data,
// as both documentGroup alone and documentGroup.ToJson()
// Also, loop through and add it to a List and return as a JsonResult
// Also, attempted to serialise, and even change the JsonWriterSettings.
}
When I look in the Immediate Window at documentGroup, it looks exactly like Json, but when I send to browser, it is an escaped string, with \" surrounding all my keys and values.
I have attempted to create a model...
public class FolderTree
{
public string id { get; set; }
public string text { get; set; }
public List<FolderTree> children { get; set; }
}
then loop through the documentGroup
foreach(var docItem in documentGroup)
{
myDocs.Add(BsonSerializer.Deserialize<FolderTree>(docItem));
}
but Bson complains that it cannot convert int to string. (I have to have text and id as a string, as some of the items are strings)
How do I get my MongoDB data output as Json, and delivered to my browser as Json?
Thanks for your assistance.
========= EDIT ===========
I have attempted to follow this answer as suggested by Yong Shun below, https://stackoverflow.com/a/43220477/4541217 but this failed.
I had issues, that the "id" was not all the way through the tree, so I changed the folder tree to be...
public class FolderTree
{
//[BsonSerializer(typeof(FolderTreeObjectTypeSerializer))]
//public string id { get; set; }
[BsonSerializer(typeof(FolderTreeObjectTypeSerializer))]
public string text { get; set; }
public List<FolderTreeChildren> children { get; set; }
}
public class FolderTreeChildren
{
[BsonSerializer(typeof(FolderTreeObjectTypeSerializer))]
public string text { get; set; }
public List<FolderTreeChildren> children { get; set; }
}
Now, when I look at documentGroup, I see...
[0]: {Plugins.Models.FolderTree}
[1]: {Plugins.Models.FolderTree}
To be fair to sbc in the comments, I have made so many changes to get this to work, that I can't remember the code I had that generated it.
Because I could not send direct, my json result was handled as...
JsonResult json = new JsonResult();
json.Data = documentGroup;
//json.Data = JsonConvert.SerializeObject(documentGroup);
json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return json;
Note, that I also tried to send it as...
json.Data = documentGroup.ToJson();
json.Data = documentGroup.ToList();
json.Data = documentGroup.ToString();
all with varying failures.
If I leave as documentGroup, I get {Current: null, WasFirstBatchEmpty: false, PostBatchResumeToken: null}
If I do .ToJson(), I get "{ \"_t\" : \"AsyncCursor`1\" }"
If I do .ToList(), I get what looks like Json in json.Data, but get an error of Unable to cast object of type 'MongoDB.Bson.BsonInt32' to type 'MongoDB.Bson.BsonBoolean'.
If I do .ToString(), I get "MongoDB.Driver.Core.Operations.AsyncCursor`1[MongoDB.Bson.BsonDocument]"
=========== EDIT 2 =================
As this way of extracting the data from MongoDB doesn't want to work, how else can I make it work?
I am using C# MVC4. (.NET 4.5.2)
I need to deliver json to the browser, hence why I am using a JsonResult return type.
I need to use an aggregate to collect from MongoDB in the format I need it.
My Newtonsoft.Json version is 11.0.2
My MongoDB.Driver is version 2.11.1
My method is the simplest it can be.
What am I missing?
Let me preface by saying I'm very new to C# development so if the solution seems obvious I apologize.
I'm getting a JSON string back from a user and I need to filter a list of C# objects based on what the JSON string contains. The JSON can only have fields that my C# model has but I don't know what fields the JSON string will contain. My C# model looks something like this:
public class Enrollment {
public int Year { get; set; }
public int NumEnrolls { get; set; }
public int DaysIntoEnrollment { get; set; }
public string Name { get; set; }
}
The JSON will have one or more of these properties with values to filter out. It could look like this:
{
"Year": ["2020", "2019"],
"Name": ["CourseA", "CourseB"],
"DaysIntoEnrollment": "20"
}
I need to filter my list of Enrollment objects based on the above JSON. So I would want the end result to have all Enrollment objects that don't contain a Year of 2020 or 2019 for example.
I've gotten a filter to work with linq on a single property but my real model has much more properties that can be filtered and I'm looking for a compact solution that will work regardless of which properties are included in the JSON. This is what I have working
public void GetFilteredData(string filters) {
var enrollList = new List<Enrollments>(); // Pretend this contains a list of valid Enrollment data
var json = JObject.Parse(filters); // filters string is in the json format from above
var propsToFilter =
from p in json["Year"]
select p;
var filtered = enrollList.Where(e => !propsToFilter.Contains(e.Year.ToString())));
}
Is there a simple way to do this without manually going through each property like I did above?
I am new in json. I want information of different users and add them to a dataGridView or dataTable or dataSet in c# (.net development). Information sample is (The json is valid):
{
"JrPwbApfIHbQhCUmVIoiVJcPYv93": {
"address": "Jessore",
"name": "Dev"
},
"iBRZAyn8TQTOgKTcByGOvJjL9ZB3": {
"address": "Bogra",
"name": "Kumar Saikat"
}
}
I want them like this :
User1 | Jessore | Dev
User2 | Bogra | Kumar Saikat
Even it would help if I could make a list for all of them.
I believe I was able to deserialise them (not sure at all) by
var model = JsonConvert.DeserializeObject<user>(json);
where user is a class.
public class Item
{
public string name;
public string address;
}
from this question-answer. From this tutorial I am able to get values if property is known. But in my case my property would be unknown, (string "User1","User2" would be random, since I will get them from a database). Any help and light on this matter would be greatly appreciated. Thank you in advance.
You're looking at a JSON dictionary, so just deserialize it as such:
public static Dictionary<string,Item> ParseJson(string source)
{
return JsonConvert.DeserializeObject<Dictionary<string,Item>>(source);
}
If you call it like this:
public static void Main()
{
var input = #"{'JrPwbApfIHbQhCUmVIoiVJcPYv93': {'address': 'Jessore','name': 'Dev' }, 'iBRZAyn8TQTOgKTcByGOvJjL9ZB3': {'address': 'Bogra','name': 'Kumar Saikat'}}";
var result = ParseJson(input);
foreach (var r in result)
{
Console.WriteLine("Key={0};Name={1};Address={2}", r.Key, r.Value.name, r.Value.address);
}
}
The output is:
Key=JrPwbApfIHbQhCUmVIoiVJcPYv93;Name=Dev;Address=Jessore
Key=iBRZAyn8TQTOgKTcByGOvJjL9ZB3;Name=Kumar Saikat;Address=Bogra
This example dumps the list to the console, but you could easily modify the for loop to add to a list instead.
See my example on DotNetFiddle
Can use the nuget package Newtonsoft.Json. This code gives you what you are looking for:
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
var json =
"{\"JrPwbApfIHbQhCUmVIoiVJcPYv93\":{\"address\":\"Jessore\",\"name\":\"Dev\"}," +
"\"iBRZAyn8TQTOgKTcByGOvJjL9ZB3\":{\"address\":\"Bogra\",\"name\":\"Kumar Saikat\"}}";
var o = JsonConvert.DeserializeObject(json);
var items = new List<Item>();
foreach (dynamic x in o as IEnumerable)
{
var i = new Item();
var y = x.First;
i.Name = y.name.Value;
i.Address = y.address.Value;
items.Add(i);
}
}
public class Item
{
public string Name { get; set; }
public string Address { get; set; }
}
}
}
Your situation is a bit strange as those autocreated names like
"JrPwbApfIHbQhCUmVIoiVJcPYv93" or else it's easier, but should be fairly easy code.
Keep in mind I use "dynamic" there which means problems will hit you at runtime NOT design time as it's not checked.
The correct way to deserialize would be as below
var model = JsonConvert.DeserializeObject<Dictionary<string, Item>>(data);
In the code sample you have posted, your "user" class name is Item but you are trying to deserialize using "User" in your code. Also please note that you cannot directly directly deserialize data into users list as it is present as a value of some random strings.
var model = JsonConvert.DeserializeObject<user>(json);
For your code to deserialize correctly, your json format should be as below :
{
{
"address": "Jessore",
"name": "Dev"
},
{
"address": "Bogra",
"name": "Kumar Saikat"
}
}
I tried to find a solution for this but couldn't. Here's the problem:
I'm loading data of a bunch of users and creating an object for each user. Each user object has many object properties. Here's the structure:
public class User {
public int ID { get; set; }
public string Name { get; set; }
public City City { get; set; }
public Office Office { get; set; }
}
The City class:
public class City {
public int ID { get; set; }
public string Name { get; set; }
public string Keyword { get; set; }
}
The Office class:
public class Office {
public int ID { get; set; }
public string Address { get; set; }
public int CityID { get; set; }
}
The user object has many other similar properties like City & Office which are basically class objects.
Now here's the main issue. Whenever I try to load all the users into a collection of dictionary, StackOverflow exception occurs at SqlCon.Open() (See the "Fetch" function I've written below). Here's how I'm loading everything:
//Code to load users
Dictionary<int, User> Users = new Dictionary<int, Users>();
DataTable usersData = new DataTable();
//The Fetch function has two version. The first one; which is mentioned in this post, returns the result as Dictionary<string, object>().
//The second version of the function returns the result in the form of the a DataTable and is only used when multiple rows are required from the database. The following returns a set of rows in a DataTable.
Globals.MainDatabase.Fetch("SELECT * FROM users", out usersData);
foreach (DataRow row in usersData.Rows) {
User user = new User();
user.ID = Convert.ToInt32(row["id"]);
user.Name = row["name"].ToString();
user.City = Cities.Get(Convert.ToInt32(row["city_id"]));
user.Office = Offices.Get(Convert.ToInt32(row["office_id"]));
Users.Add(user.ID, user);
}
The methods "Cities.Get(Int32 id)" and "Offices.Get(Int32 id)" uses the following function to fetch data from the database.
public void Fetch(string query, out Dictionary<string, object> results) {
var dict = new Dictionary<string, object>();
try {
using (SqlConnection SqlCon = new SqlConnection(ConnectionString)) {
using (SqlCmd = new SqlCommand()) {
SqlCmd.Connection = SqlCon;
SqlCmd.CommandType = CommandType.Text;
SqlCmd.CommandText = query;
SqlCon.Open();
DataTable temp = new DataTable();
using (SqlDataAdapter SqlAdp = new SqlDataAdapter(SqlCmd)) {
SqlAdp.SelectCommand = SqlCmd;
SqlAdp.Fill(temp);
}
DataRow row = temp.Rows[0];
temp = null;
dict = row.Table.Columns
.Cast<DataColumn>()
.ToDictionary(col => col.ColumnName, col => row.Field<object>(col.ColumnName));
}
}
}
catch (Exception ex) {
HandleException(ex, "An error occurred when tried to fetch data.", query);
}
results = dict;
dict = null;
}
I realize that this "Fetch" function is being called multiple times when creating the user object. The "StackOverflow" exception occurs exactly at this line:
SqlCon.Open();
How can I solve this error? or probably I should use a better approach to do this?
A bit too long for a comment
Do you really need to load ALL data from the database? It is better to just grab the columns and rows you need.
Why are you copying DataTables in to Dictionaries? What is wrong with just using a DataTable?
99.9% of the time you will have better performance performing JOINs in the database.
Don't try to roll your own 'ORM'. Use something like Dapper if you don't want the bloat of EF or NHibernate. Or, stick to ADO (DataTable, DataAdapter etc.)
using (SqlConnection SqlCon = new SqlConnection(ConnectionString)) {
Is your connection string variable really named ConnectionString? Is it possible you are having a name clash with the type? Since it is not declared in the shown code, I assume it is a class variable, so you should respect the conventional naming convention, which would be _connectionString. What does your connection string look like?
Alright folks, I figured it out. It was all because of faulty architecture of the whole demo application. Some objects have one or more other objects as properties and due to some silly faults in the architecture; the "fetch" operation which serves as the base of fetching data from the database, was recursively called resulting into StackOverflow exception which was actually a large number of database connections being initialized ultimately growing the heap size to an extent which causes the exception.
I tried to summarize everything in the paragraph written above because posting the complete source code is useless considering the large amount of code.
Thank you everyone who helped, especially #Guffa's comment on the main post which forced me to investigate the whole issue from scratch rather than sticking to the exception stack.
i want create a custom json data from the mssql 2008 results so that my iPhone could consume it.
i can consume json websites like this without a problem:
json link
the format could look like this:
Place: New York
Hotel: Widget Hotel
Telephone: 0305525253
which format should i return to consume it on the iPhone? an array a string, dictionary? i don't know
EDIT: here is my code:
namespace WebService1
{
public class LandHelper
{
internal static string[] Land()
{
List<string> landObject = new List<string>();
using (SqlConnection con = new SqlConnection(#"Data Source=Localhost\SQLEXPRESS;Initial Catalog=BOOK-IT-V2;Integrated Security=true;"))
using (SqlCommand cmd = new SqlCommand(#"SELECT BEZEICHNUNG FROM LAND", con))
{
con.Open();
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
if (rdr["BEZEICHNUNG"] != DBNull.Value)
{
landObject.Add(rdr["BEZEICHNUNG"].ToString());
}
}
}
}
return landObject.ToArray();
}
}
}
i tried this but i don't know how to add a key to the values i get from the database.
i think it would be easier when i create a custom json format .
Just Return a List like
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public List<StandortHelper.REGION> LAND()
{
return StandortHelper.LAND();
}
You can return easy a JSON-FORMAT
Okay, if I've understood this correctly what you're trying to do is stringify a collection of objects to JSON, and then read them in your iPhone app. I'm assuming you're asking for how to convert from object-to-JSON, and that you've already covered JSON-to-object. I'll cover object-to-JSON here.
What you'll need to do is create a class that contains all of the information you need in your JSON file, like so.
public class Location
{
public string Place { get; set; }
public string Hotel { get; set; }
public string Telephone { get; set; }
}
Now, inside your database reader statement, you'll want to construct a List<Location>, and then add each Location to this list. Once you've done this, you'll easily be able to convert the collection to JSON using the JSON.NET framework... which makes converting objects to JSON as easy as:
// "locations" is the name of the collection
JsonConvert.SerializeObject(locations);
JSON .NET Documentation
Hope this helps. :)