我试图使用一个API,使用以下示例结构为他们返回的json
[
{
"customer":{
"first_name":"Test",
"last_name":"Account",
"email":"test1@example.com",
"organization":"",
"reference":null,
"id":3545134,
"created_at":"2013-08-06T15:51:15-04:00",
"updated_at":"2013-08-06T15:51:15-04:00",
"address":"",
"address_2":"",
"city":"",
"state":"",
"zip":"",
"country":"",
"phone":""
}
},
{
"customer":{
"first_name":"Test",
"last_name":"Account2",
"email":"test2@example.com",
"organization":"",
"reference":null,
"id":3570462,
"created_at":"2013-08-12T11:54:58-04:00",
"updated_at":"2013-08-12T11:54:58-04:00",
"address":"",
"address_2":"",
"city":"",
"state":"",
"zip":"",
"country":"",
"phone":""
}
}
]
Run Code Online (Sandbox Code Playgroud)
JSON.net可以使用类似以下结构的东西
{
"customer": {
["field1" : "value", etc...],
["field1" : "value", etc...],
}
}
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何让它对提供的结构感到满意.
使用默认的JsonConvert.DeserializeObject(content)会生成正确的Customer数,但所有数据都为null.
在CustomerList(下面)执行某些操作会导致"无法反序列化当前JSON数组"异常
public class CustomerList
{
public List<Customer> customer { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
思考?
我正在寻找一个示例代码/ lib来使用C#解码JSON字符串.
编码我可以这样做:
var data = new Dictionary<string,string>();
data.Add("..", "...");
var json_encoded = new JavaScriptSerializer().Serialize(data);
Run Code Online (Sandbox Code Playgroud)
但我怎么解码?
var json_decoded = ??
Run Code Online (Sandbox Code Playgroud) 我有一个json字符串,它是通过序列化一个对象数组创建的:
[
{
"html": "foo"
},
{
"html": "bar"
}
]
Run Code Online (Sandbox Code Playgroud)
如何将其反序列化为某个可迭代的C#结构?我试过这段代码,但是我收到了No parameterless constructor defined for type of 'System.String'.错误:
string[] htmlArr = new JavaScriptSerializer().Deserialize<String[]>(html);
Run Code Online (Sandbox Code Playgroud)
我想要获得的是一个可迭代的结构来获取每个'html'对象.
我正在使用ObjectForScripting属性与WebBrowser控件内的网页进行交互,一切正常,但我无法弄清楚如何将字符串数组传递回C#
HTML代码
<input type="submit" onclick="window.external.save(Array('test', 'test2'))" />
Run Code Online (Sandbox Code Playgroud)
形成
// Returns System.__ComObject
public void Save(object parameters)
{
}
// Throws an exception
public void Save(object[] parameters)
{
}
// Also throws an exception
public void Save(string[] parameters)
{
}
Run Code Online (Sandbox Code Playgroud) 假设我的json存储在字符串变量中.我的json看起来像
"{\"file\":\"dave\",\"type\":\"ward\"}"
Run Code Online (Sandbox Code Playgroud)
上面的数据存储在我的字符串变量中.现在我如何反序列化这个json数据并读取每个组件,如文件和类型.
我尝试使用以下代码但收到错误
var jss = new JavaScriptSerializer();
string json = new StreamReader(context.Request.InputStream).ReadToEnd();
var sData=jss.Deserialize<string>(json);
string _file = sData[0].ToString();
string _type = sData[1].ToString();
Run Code Online (Sandbox Code Playgroud)
请指导我如何从json格式的数据反序列化后,如何提取文件和类型等每个组件.谢谢
纠正
这样我解决了这个....这里是整个客户端和服务器端代码.
<script type="text/javascript">
$(document).ready(function () {
$('#btnlogin').click(function (e) {
var FeedCrd = {};
FeedCrd["file"] = 'dave';
FeedCrd["type"] = 'ward';
$.ajax({
type: "POST",
url: "CallASHX.ashx",
data: JSON.stringify(FeedCrd),
//data: { 'file': 'dave', 'type': 'ward' },
//dataType: "json",
success: function (data) {
if (data == "SUCCESS");
{
alert('SUCCESS');
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus); …Run Code Online (Sandbox Code Playgroud) I'm trying to work with the google maps api. This URL is EXACTLY what I need:
http://maps.googleapis.com/maps/api/geocode/json?address=77379
Take a look at the results. It has all the info I need... lat, lon, state, country. Trouble is, I don't know how to extract this data. I tried this:
var client = new WebClient();
var content = client.DownloadString("http://maps.googleapis.com/maps/api/geocode/json?address=77379");
object myObject = JsonConvert.DeserializeObject(content);
Run Code Online (Sandbox Code Playgroud)
While that doesn't error out, myObject doesn't end up being anything useful. (Or, maybe it is and I just don't …
我使用的API返回具有一些属性的公司符号的集合,但是我不知道如何反序列化它
{
"A": {
"advanced-stats": {
"prop1": 0.198791,
"prop2": 16.59,
"prop3": 12.44,
}
},
"AA": {
"advanced-stats": {
"prop1": 0.198791,
"prop2": 16.59,
"prop3": 12.44,
}
},
"AAAU": {
"advanced-stats": {
"prop1": 0.198791,
"prop2": 16.59,
"prop3": 12.44,
}
}
}
Run Code Online (Sandbox Code Playgroud) 对我的网络请求的响应如下(不在我的控制之下):
{
"nasdaq_imbalance":
{
"name": "nasdaq_imbalance",
"group": "Market Data",
"description": null
},
"DXOpen IM":
{
"name": "DXOpen IM",
"group": "Daily",
"description": null
},
"Float Shares":
{
"name": "Float Shares",
"group": "Daily",
"description": null
},
Run Code Online (Sandbox Code Playgroud)
}
不知何故,我需要将它反序列化为包含对象列表的 C# 对象......基本上我需要一个这样的对象列表:
public class Dataset {
public string name { get; set; }
public string group { get; set; }
public string description { get; set; }
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试解析一个JsonArray很多个小时,我通过graph.facebook得到了,所以我可以得到额外的值.我想要提取的值是消息和ID.
获得JasonArry是没问题的并且工作正常:
[
{
"code":200,
"headers":[{"name":"Access-Control-Allow-Origin","value":"*"}],
"body":"{
\"id\":\"255572697884115_1\",
\"from\":{
\"name\":\"xyzk\",
\"id\":\"59788447049\"},
\"message\":\"This is the first message\",
\"created_time\":\"2011-11-04T21:32:50+0000\"}"},
{
"code":200,
"headers":[{"name":"Access-Control-Allow-Origin","value":"*"}],
"body":"{
\"id\":\"255572697884115_2\",
\"from\":{
\"name\":\"xyzk\",
\"id\":\"59788447049\"},
\"message\":\"This is the second message\",
\"created_time\":\"2012-01-03T21:05:59+0000\"}"}
]
Run Code Online (Sandbox Code Playgroud)
现在我已经尝试了几种方法来访问消息,但每个方法都以catch结尾...并抛出异常.
例如:
var serializer = new JavaScriptSerializer();
var result = serializer.Deserialize<dynamic>(json);
foreach (var item in result)
{
Console.WriteLine(item.body.message);
}
Run Code Online (Sandbox Code Playgroud)
抛出异常:System.Collections.Generic.Dictionary不包含body的定义.不过你在下面的截图中看到,该主体包含定义.
Becaus我不允许发布你可以在directupload上找到的图片:http://s7.directupload.net/images/120907/zh5xyy2k.png
我没有更多的想法,所以我请你帮助我.我需要这个项目,私有,而不是商业.
也许你可以给我一个代码短语,所以我可以继续我的发展.
谢谢你到目前为止
多米尼克
嗨我收到了来自网络服务的回复,我希望从回复中获得一个网址.我的回答是以下格式.
[{"cdn_streaming_uri": "9e849cfbb2e157-22558a0600b387d0abe240fe5.r73.stream..rackcdn.com", "name": "test1", "cdn_ios_uri": "d3d4c27-22558a0600b387d0abc071d0ae240kcdn.com", "cdn_ssl_uri": "https://990fea26e-22558a0600b387d0abc071d0ae240fe5.ssl.cdn.com", "cdn_enabled": false, "ttl": 259200, "log_retention": false, "cdn_uri": "99b56a009-22558a0600b3c071d0ae240fe5.r73.n.com"}, {"cdn_streaming_uri": "74ec8c-d5edc6cad91792413b1b134fde.r46.stcdn.com", "name": "test2", "cdn_ios_uri": "d05437e44-d5edc61792413b1b134fde.iosr.cdn.com", "cdn_ssl_uri": "https://a1c2ebbf5-d5edc6cd91792413b1b134fde.scdn.com", "cdn_enabled": false, "ttl": 259200, "log_retention": false, "cdn_uri": "72ffd-d5edc6ca16852413b1b134fde.cdn.com"}, {"cdn_streaming_uri": "93665b76-550971032c2a22cdn.com", "name": "test3", "cdn_ios_uri": "ca6b-550971032c2fbf19452d6a.iosr.cf2.rackcdn.com", "cdn_ssl_uri": "https://c7c39-550971032cbf19452d6cdn.com", "cdn_enabled": true, "ttl": 86400, "log_retention": true, "cdn_uri": "68fc6d831a94-550971032c252d6a.r3cdn.com"}]
Run Code Online (Sandbox Code Playgroud)
我需要"cdn_streaming_uri"作为名称"test3".
您可以在http://json.parser.online.fr/中查看JSON解析器. 如何解析它?
这是我的代码:
public static object getTokenResponse(String PrivateURL, string ResponseType)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(PrivateURL +"?format=JSON");
request.Method = "GET";
request.Headers.Add("X-Auth-Token", id);
//request.ContentType = "application/XML";
HttpWebResponse resp1;
try
{ …Run Code Online (Sandbox Code Playgroud) c# ×10
json ×7
.net ×2
asp.net ×2
asp.net-core ×1
com ×1
converters ×1
facebook ×1
google-maps ×1
interop ×1
jquery ×1
json.net ×1
parsing ×1
winforms ×1