如何在数组json的数组json中存储对象json? [英] How can I store object json in array json with array name ?
问题描述
//This is my code and i want to pass api with array name please give me the answer
protected void Page_Load(object sender, EventArgs e)
{
//IsTrue = 0;
if (string.IsNullOrEmpty(Request.QueryString["Username"]) == false
&& string.IsNullOrEmpty(Request.QueryString["Password"]) == false)
//{
// IsTrue = 1;
//}
//if (IsTrue > 0)
{
DataTable dt = new DataTable();
UserName = Request.QueryString["UserName"];
Password = Request.QueryString["Password"];
con.Open();
dt.Load(new SqlCommand("select Top 10 Memberid,MemberName from PartyMaster Order by msrno", con).ExecuteReader());
con.Close();
System.Web.Script.Serialization.JavaScriptSerializer serializer
= new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row = default(Dictionary<string, object>);
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
}
//string Mystr = serializer.Serialize(rows);
string Mystr = serializer.Serialize(rows);
Response.ContentType = "application/json"; //JSON Text output
Response.Write(Mystr);
}
我的尝试:
What I have tried:
How can i store Object Json in Array Json with Array name ?
请给我答案
please give me the answer
推荐答案
我在json.net图书馆上发表演讲
在以下链接
exmaple of你想要什么
i prefare the json.net libraray
more information about it found in the following link
exmaple of what you want
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
public class Product
{
public string Name
{
get;
set;
}
public DateTime Expiry
{
get;
set;
}
public string[] Sizes
{
get;
set;
}
}
public class Program
{
public static void Main()
{
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[]{"Small"};
List<Product> productsArr = new List<Product>{
product,
product
};
var dataObj = new
{
Data = productsArr
}
;
string json = JsonConvert.SerializeObject(dataObj);
Console.WriteLine(json);
}
}
the following link
其实先生我想要显示我的序列化数据 - 数据:[{demo1:123}}]我正在使用查询字符串default.aspx?username = abc&调试此代码后,密码= ayz,现在我的数据显示为 - [{demo1:123}]
Actually sir i want show my serialize data just like that - "Data":[{"demo1":123}}] and I'm using query string default.aspx?username=abc & password=ayz after debugging this code, now my data show like that - [{"demo1":123}]
这应该在问题正文中,而不是在下面的注释中。
首先,您的JSON示例无效。您可以在此处验证您的JSON: JSON Formatter&验证器 [ ^ ]
更正为:
This should be in the question body and not in the comments below.
Firstly, your JSON example is invalid. You can validate your JSON here: JSON Formatter & Validator[^]
Correction is:
{
"Data":[
{
"demo1":123
}
]
}
下一页,您需要有一个类结构来存储要转换的数据。我最喜欢的工具是: JSON Utils:从JSON生成C#,VB.Net,SQL表,Java和PHP [ ^ ]
使用此工具生成以下类:
Next, you need to have a class structure to store the data to be converted. My favorite tool is: JSON Utils: Generate C#, VB.Net, SQL Table, Java and PHP from JSON[^]
Using this tool the following classes are generated:
public class Datum
{
[JsonProperty("demo1")]
public int Demo1 { get; set; }
}
public class Example
{
[JsonProperty("Data")]
public IList<Datum> Data { get; set; }
}
现在要处理转换,TheSniper105上面有一个使用Newtonsoft的Json.NET库的例子。这是我的帮助类,可以在我的文章 在C#中使用JSON中找到&安培; VB [ ^ ]:
Now to handle conversion, TheSniper105 has an example above using Newtonsoft's Json.NET library. Here is my helper class, found in my article Working with JSON in C# & VB[^]:
using Newtonsoft.Json;
using System.Collections.Generic;
namespace Support.CSharp
{
public static class JsonHelper
{
public static string FromClass<T>(T data, bool isEmptyToNull = false,
JsonSerializerSettings jsonSettings = null)
{
string response = string.Empty;
if (!EqualityComparer<T>.Default.Equals(data, default(T)))
response = JsonConvert.SerializeObject(data, jsonSettings);
return isEmptyToNull ? (response == "{}" ? "null" : response) : response;
}
public static T ToClass<T>(string data,
JsonSerializerSettings jsonSettings = null)
{
var response = default(T);
if (!string.IsNullOrEmpty(data))
response = jsonSettings == null
? JsonConvert.DeserializeObject<T>(data)
: JsonConvert.DeserializeObject<T>(data, jsonSettings);
return response;
}
}
}
使用上面的助手类进行序列化:
And to serialize using the above helper class:
var sampleClass = new Example { Data = new List<Datum> { new Datum { Demo1 = 123 }}};
var jsonData = JsonHelper.FromClass(sampleClass);
并反序列化:
And to deserialize:
var jsonData = "{"Data":[{"demo1":123}]}";
var sampleClass = JsonHelper.ToClass<Example>(jsonData);
您可以了解更多来自我的文章 在C#中使用JSON& VB [ ^ ]。
我创建了一个读取并转换为Dictionary的方法。您可以根据需要进行更改。我的代码如下:
I have created a method to read and convert it to Dictionary. You can change it according to your need. My code is as follows:
public static Dictionary<string, string> ValidateJsonRequiredValue(JObject jsonData, string[] parameters, string[] nonMandatoryParam)
{
if (jsonData == null || !jsonData.HasValues)
{
return null;
}
Dictionary<string, string> parametersValue = new Dictionary<string, string>();
string retValue;
foreach (string key in parameters)
{
retValue = GetJsonParameterValue(jsonData, key);
if (retValue == null)
return null;
parametersValue.Add(key, retValue);
}
if (nonMandatoryParam == null)
return parametersValue;
foreach (string key in nonMandatoryParam)
{
retValue = GetJsonParameterValue(jsonData, key);
parametersValue.Add(key, retValue);
}
return parametersValue;
}
public static string GetJsonParameterValue(JObject jsonData, string parameterName)
{
string retValue = string.Empty;
if (jsonData != null)
{
JToken obj = jsonData.GetValue(parameterName, StringComparison.CurrentCultureIgnoreCase);
return obj == null ? null : Convert.ToString(obj);
}
return retValue;
}
这篇关于如何在数组json的数组json中存储对象json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!