xml to json (asp.net)

qqzeng-ip 2010-03-06 11:14:17
xml数据:
http://rainmeter.accu-weather.com/widget/rainmeter/weather-data.asp?location=ASI|CN|CH007|YULIN&metric=c

如何获取如下json数据:


asp.net 如何取得如下:

{
"city":"Yulin",
"curr_temp":72,
"curr_text":"Mostly Sunny",
"curr_icon":2,

"forecast":[{
"day_date":"3\/6\/2010",
"day_text":"Sunny and very warm",
"day_icon":1,
"day_htemp":87,
"day_ltemp":65
},
{"day_date":"3\/7\/2010",
"day_text":"Not as warm with low clouds",
"day_icon":8,
"day_htemp":69,
"day_ltemp":60},

{"day_date":"3\/8\/2010",
"day_text":"Cloudy with spotty showers",
"day_icon":12,
"day_htemp":69,
"day_ltemp":48},

{"day_date":"3\/9\/2010",
"day_text":"Breezy and colder withrain",
"day_icon":18,
"day_htemp":53,
"day_ltemp":43},

{"day_date":"3\/10\/2010",
"day_text":"Sunny and warmer",
"day_icon":1,
"day_htemp":59,
"day_ltemp":51}

]}
...全文
234 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
qqzeng-ip 2010-03-07
  • 打赏
  • 举报
回复
up up
hanbquan 2010-03-07
  • 打赏
  • 举报
回复
引用 1 楼 sp1234 的回复:
.net framework中分别拥有xml和json的序列化框架类。

这楼对框架很精通啊
liyoubaidu 2010-03-07
  • 打赏
  • 举报
回复
不知道是不是叫linq 回复内容
liyoubaidu 2010-03-07
  • 打赏
  • 举报
回复
不知道是不是叫linq
qqzeng-ip 2010-03-07
  • 打赏
  • 举报
回复
如何取十楼 对应数据?
xray2005 2010-03-07
  • 打赏
  • 举报
回复
.NET Framework对于JSON本提供了很好的支持。

1. 使用JavaScriptSerializer,位于命名空间System.Web.Script.Serialization,使用如下:

序列化为JSON字符串:

C#代码

User user = new User {Name = "jquery", Age = 20};
JavaScriptSerializer serializer = new JavaScriptSerializer();
string result = serializer.Serialize(user);

反序列化JSON

C#代码

String input = "";
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.Deserialize(input);

2. 使用DataContractJsonSerializer类,

推荐使用这个,这是最新的,位于命名空间System.Runtime.Serialization.Json下,序列化和反序列化分别调用它的WriteObject()和ReadObject()方法就可以了。
qqzeng-ip 2010-03-07
  • 打赏
  • 举报
回复
$weather['city'] = (string)$xml->local->city;//城市
$weather['curr_temp'] = (int)$xml->currentconditions->temperature;//气温
$weather['curr_text'] = (string)$xml->currentconditions->weathertext;//天气
$weather['curr_icon'] = (int)$xml->currentconditions->weathericon;// 03

// forecast
$day = count($xml->forecast->day);
for ($i = 0; $i < $day; $i++) {
$weather['forecast'][$i]['day_date'] = (string)$xml->forecast->day[$i]->obsdate;// 3/7/2010
$weather['forecast'][$i]['day_text'] = (string)$xml->forecast->day[$i]->daytime->txtshort;// 天气
$weather['forecast'][$i]['day_icon'] = (int)$xml->forecast->day[$i]->daytime->weathericon;// 08
$weather['forecast'][$i]['day_htemp'] = (int)$xml->forecast->day[$i]->daytime->hightemperature;// 69
$weather['forecast'][$i]['day_ltemp'] = (int)$xml->forecast->day[$i]->daytime->lowtemperature;// 57
}

echo json_encode($weather);


转换成.net有什么好方法?
cpp2017 2010-03-06
  • 打赏
  • 举报
回复
你只要哪部分数据?做个条件筛选就可以了。
qqzeng-ip 2010-03-06
  • 打赏
  • 举报
回复
我可以序列化出来 但是获取想要的部分数据 有没有快捷的办法 ? 我看php就是七八行代码就搞掂了
  • 打赏
  • 举报
回复
.net framework中分别拥有xml和json的序列化框架类。
qqzeng-ip 2010-03-06
  • 打赏
  • 举报
回复
不够分再加 有的是分...
MicroDeviser 2010-03-06
  • 打赏
  • 举报
回复
我不是高手,帮你顶~~~~~~~
qqzeng-ip 2010-03-06
  • 打赏
  • 举报
回复
xml 高 手 呢
qqzeng-ip 2010-03-06
  • 打赏
  • 举报
回复
up 回复内容太短了!
qqzeng-ip 2010-03-06
  • 打赏
  • 举报
回复
如何遍历xml里面 forecast 里面

$day = count($xml->forecast->day);
for ($i = 0; $i < $day; $i++) {
->forecast->day[$i]->obsdate;
->forecast->day[$i]->daytime->txtshort;
->forecast->day[$i]->daytime->weathericon;
->forecast->day[$i]->daytime->hightemperature;
->forecast->day[$i]->daytime->lowtemperature;
}

用c# 怎么来呢

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧