Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 252 Vote(s) - 3.4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create JSON string in C#

#11
I've found that you don't need the serializer at all. If you return the object as a List<type>.
Let me use an example.

In our asmx we get the data using the variable we passed along

// return data
[WebMethod(CacheDuration = 180)]
public List<latlon> GetData(int id)
{
var data = from p in db.property
where p.id == id
select new latlon
{
lat = p.lat,
lon = p.lon

};
return data.ToList();
}

public class latlon
{
public string lat { get; set; }
public string lon { get; set; }
}

Then using jquery we access the service, passing along that variable.

// get latlon
function getlatlon(propertyid) {
var mydata;

$.ajax({
url: "getData.asmx/GetLatLon",
type: "POST",
data: "{'id': '" + propertyid + "'}",
async: false,
contentType: "application/json;",
dataType: "json",
success: function (data, textStatus, jqXHR) { //
mydata = data;
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
console.log(xmlHttpRequest.responseText);
console.log(textStatus);
console.log(errorThrown);
}
});
return mydata;
}

// call the function with your data
latlondata = getlatlon(id);

And we get our response.

{"d":[{"__type":"MapData+latlon","lat":"40.7031420","lon":"-80.6047970}]}

Reply

#12
Using [Newtonsoft.Json][1] makes it really easier: <br/>

Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };

string json = JsonConvert.SerializeObject(product);

Documentation: **[Serializing and Deserializing JSON][2]**


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#13

***Encode Usage***

Simple object to JSON Array EncodeJsObjectArray()

public class dummyObject
{
public string fake { get; set; }
public int id { get; set; }

public dummyObject()
{
fake = "dummy";
id = 5;
}

public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append('[');
sb.Append(id);
sb.Append(',');
sb.Append(JSONEncoders.EncodeJsString(fake));
sb.Append(']');

return sb.ToString();
}
}

dummyObject[] dummys = new dummyObject[2];
dummys[0] = new dummyObject();
dummys[1] = new dummyObject();

dummys[0].fake = "mike";
dummys[0].id = 29;

string result = JSONEncoders.EncodeJsObjectArray(dummys);

Result:
[[29,"mike"],[5,"dummy"]]

Pretty Usage

Pretty print JSON Array PrettyPrintJson() string extension method

string input = "[14,4,[14,\"data\"],[[5,\"10.186.122.15\"],[6,\"10.186.122.16\"]]]";
string result = input.PrettyPrintJson();


Results is:

[
14,
4,
[
14,
"data"
],
[
[
5,
"10.186.122.15"
],
[
6,
"10.186.122.16"
]
]
]
Reply

#14
Include:

using System.Text.Json;

Then serialize your object_to_serialize like this:
JsonSerializer.Serialize(object_to_serialize)
Reply

#15
Simlpe use of **Newtonsoft.Json** and **Newtonsoft.Json.Linq** libraries.

//Create my object
var myData = new
{
Host = @"sftp.myhost.gr",
UserName = "my_username",
Password = "my_password",
SourceDir = "/export/zip/mypath/",
FileName = "my_file.zip"
};

//Tranform it to Json object
string jsonData = JsonConvert.SerializeObject(myData);

//Print the Json object
Console.WriteLine(jsonData);

//Parse the json object
JObject jsonObject = JObject.Parse(jsonData);

//Print the parsed Json object
Console.WriteLine((string)jsonObject["Host"]);
Console.WriteLine((string)jsonObject["UserName"]);
Console.WriteLine((string)jsonObject["Password"]);
Console.WriteLine((string)jsonObject["SourceDir"]);
Console.WriteLine((string)jsonObject["FileName"]);
Reply

#16
If you want to avoid creating a class and create JSON then Create a dynamic Object and Serialize Object.

```
dynamic data = new ExpandoObject();
data.name = "kushal";
data.isActive = true;

// convert to JSON
string json = Newtonsoft.Json.JsonConvert.SerializeObject(data);

```
Read the JSON and deserialize like this:
```
// convert back to Object
dynamic output = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

// read a particular value:
output.name.Value
```

`ExpandoObject` is from `System.Dynamic` namespace.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through