Windows Phone中的fetchin json数组 [英] fetchin json array in windows phone
问题描述
{"名称":[" NAME1"," NAME2"," NAME3"," NAME4"]}
{"names":["name1","name2","name3","name4"]}
我需要取该n在列表中显示它们......我在做什么 -
i need to fetch this n show them in a list... what i ma doing is -
类 - >
公共类品牌>
{ 公共字符串名称
{get;组; }
}
公共类品牌
{&NBSP; public ObservableCollection< brand>姓名
{get;组; }¥b $ b }
public class brand
{ public string Name
{ get; set; }
}
public class brands
{ public ObservableCollection<brand> Names
{ get; set; }
}
主页
公共部分类MainPage:PhoneApplicationPage
{
//构造函数
public MainPage()
{
InitializeComponent();
getdata();
}
private void getdata()
{
string uri =" URL";
HttpWebRequest的REQ =(HttpWebRequest的)HttpWebRequest.Create(新URI(URI));
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP; req.BeginGetResponse(new AsyncCallback(show),req);
}
private void show(IAsyncResult asynchronousResult)
{
HttpWebRequest request =(HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse响应=(HttpWebResponse)request.EndGetResponse(asynchronousResult);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; StreamReader stream1 = new StreamReader(response.GetResponseStream());
$
$
string s1 = stream1.ReadToEnd();
var ser = new DataContractJsonSerializer(typeof(brands));
var stream = new MemoryStream(Encoding.Unicode.GetBytes(s1));
var bb =(brands)ser.ReadObject(stream);
string abc = bb.ToString();
品牌b =新品牌();
foreach(b.Name中的var ev)
{
textBlock1.Text = ev.ToString();
}
}
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
getdata();
}
private void getdata()
{
string uri = "URL";
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(new Uri(uri));
req.BeginGetResponse(new AsyncCallback(show), req);
}
private void show(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
StreamReader stream1 = new StreamReader(response.GetResponseStream());
string s1 = stream1.ReadToEnd();
var ser = new DataContractJsonSerializer(typeof(brands));
var stream = new MemoryStream(Encoding.Unicode.GetBytes(s1));
var bb = (brands)ser.ReadObject(stream);
string abc = bb.ToString();
brand b = new brand();
foreach (var ev in b.Name)
{
textBlock1.Text = ev.ToString();
}
}
这是我在阅读博客文章后做的..但我没有得到任何输出..
this i made after reading blog posts.. but i am not getting any output..
所以PLZ帮助我。
推荐答案
我曾经使用过这种反序列化JSON对象的方法,但这只适用于这种类型的JSON对象,在其他情况下它失败。
I used to use this kind of methods to Deserialize a JSON object, but this works in just this type of JSON Objects, in other cases it fails.
所以我建议你使用一个更通用的术语,它将在大多数情况下工作案件。
So I would suggest you using a more general term which will work in most of the cases.
它的工作原理如下:
string s = streamReader.ReadToEnd();
Dictionary< string,List< Dictionary< string,object>>> jsonObj =
Dictionary<string, List<Dictionary<string, object>>> jsonObj =
<跨度类= "x_Apple标签跨度" 样式= "空白:预"> 跨度> JsonConvert.DeserializeObject<字典<字符串列表与LT;字典<串,对象> >>>(s);
JsonConvert.DeserializeObject<Dictionary<string, List<Dictionary<string, object>>>>(s);
现在,您可以按照常用的方式遍历字典。
Now you can iterate through the Dictionary like you are always used to.
JsonConvert是来自Newtonsoft.Json命名空间的第三方类:http://james.newtonking.com/pages/json-net.aspx
JsonConvert is a third-party class from Newtonsoft.Json namespace found here: http://james.newtonking.com/pages/json-net.aspx
将对象序列化回JSON再次就像这个一样简单。希望它有所帮助
Serializing the object back to JSON is again as easy as this one. Hope it helps
这篇关于Windows Phone中的fetchin json数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!