从文件UWP(C#)读取JSON
我喜欢创建通用应用程序 我在.txt文件中有JSON。xaml中的图像和文本框 我需要从.txt读取JSON并将文本从JSON解析到TextBox。 对于图像,我需要通过url下载它,即JSON 我的JSON从文件UWP(C#)读取JSON,c#,json,visual-studio,win-universal-app,C#,Json,Visual Studio,Win Universal App,我喜欢创建通用应用程序 我在.txt文件中有JSON。xaml中的图像和文本框 我需要从.txt读取JSON并将文本从JSON解析到TextBox。 对于图像,我需要通过url下载它,即JSON 我的JSON Array ( [0] => Array ( [post_title] => Ролл Чеддер c темпура креветкой [post_excerpt] => Рис, нор
Array
(
[0] => Array
(
[post_title] => Ролл Чеддер c темпура креветкой
[post_excerpt] => Рис, нори, темпура креветка, сыр филадельфия, огурец, сыр чеддер, соус голландский
[img_url] => http://new.murakami.ua/wp-content/uploads/535_520Rol-chedr-s-sirom-filadelfiej-i-tempura-krevetkoyu.jpg
[visibility] => visible
[price] => 124.00
[weight] => 195/16
[sku] => 233
)
[1] => Array
(
[post_title] => Ролл спайси лосось
[post_excerpt] => Рис, нори, лосось, спайси соус, икра масаго, зеленый лук
[img_url] => http://new.murakami.ua/wp-content/uploads/535_520Rol-spajsi-losos.jpg
[visibility] => visible
[price] => 68.00
[weight] => 130/16
[sku] => 239
)
[2] => Array
(
[post_title] => Ролл с пастрами
[post_excerpt] => Рис, нори, пастрами, листья салата, перец болгарский, огурец, соус горчичный, помидор, голландский соус
[img_url] => http://new.murakami.ua/wp-content/uploads/535_520Rol-z-pastrami.jpg
[visibility] => visible
[price] => 98.00
[weight] => 185/16
[sku] => 245
)
[3] => Array
(
[post_title] => Ролл огуречный лосось
[post_excerpt] => Рис, нори, лосось, сыр филадельфия, сладкий омлет, икра тобико, огурец, голландский соус
[img_url] => http://new.murakami.ua/wp-content/uploads/535_520Rol-ogirkovij-losos.jpg
[visibility] => visible
[price] => 112.00
[weight] => 190/16
[sku] => 244
)
[4] => Array
(
[post_title] => Ролл оши абури с лососем и голландским соусом
[post_excerpt] => Рис, лосось, огурец, перец чили, голландский соус
[img_url] => http://new.murakami.ua/wp-content/uploads/535_520Oshi-aburi-z-lososem-i-golandskim-sousom.jpg
[visibility] => visible
[price] => 98.00
[weight] => 18016
[sku] => 240
)
我是如何通过Xamarin在Adroid中实现的
var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
var filename = System.IO.Path.Combine(path, "cache3.txt");
JsonValue readJson;
var jsonString = File.ReadAllText(filename);
readJson = JsonObject.Parse(jsonString);
private async void ParseAndDisplay1(JsonValue readJson)
{
TextView productname = FindViewById<TextView>(Resource.Id.posttittle);
TextView price = FindViewById<TextView>(Resource.Id.price);
TextView weight = FindViewById<TextView>(Resource.Id.weight);
ImageView imagen = FindViewById<ImageView>(Resource.Id.image1);
ImageButton add = FindViewById<ImageButton>(Resource.Id.add);
add.Click += delegate
{
var intent = new Intent(this, typeof(BludoDetail));
StartActivity(intent);
};
productname.Click += delegate
{
var intent485 = new Intent(this, typeof(BludoDetail));
StartActivity(intent485);
};
JsonValue firstitem = readJson[0];
productname.Text = firstitem["post_title"];
price.Text = firstitem["price"] + " грн";
weight.Text = firstitem["weight"];//"г";
Koush.UrlImageViewHelper.SetUrlDrawable(imagen, firstitem["img_url"], null, 5000000);
}
var path=System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
var filename=System.IO.Path.Combine(路径“cache3.txt”);
JsonValue-readJson;
var jsonString=File.ReadAllText(文件名);
readJson=JsonObject.Parse(jsonString);
私有异步void ParseAndDisplay1(JsonValue readJson)
{
TextView productname=FindViewById(Resource.Id.PostTitle);
TextView price=findviewbyd(Resource.Id.price);
TextView-weight=findviewbyd(Resource.Id.weight);
ImageView imagen=FindViewById(Resource.Id.image1);
ImageButton add=findviewbyd(Resource.Id.add);
添加。单击+=委派
{
var intent=新的intent(此,类型为(BludoDetail));
星触觉(意向);
};
productname。单击+=委派
{
var intent485=新的意图(此,类型为(BludoDetail));
星触觉(intent485);
};
JsonValue firstitem=readJson[0];
productname.Text=firstitem[“post_title”];
price.Text=firstitem[“price”]+“Гцц”;
weight.Text=firstitem[“weight”];/“Γ”;
Koush.urlmageviewHelper.SetUrlDrawable(imagen,firstitem[“img_url”],null,5000000);
}
在Xamarin.Android中,我有Koush插件,用于下载和缓存图像
如何在UWP中将信息从Json解析到文本框,以及如何下载图像并将其缓存?您可以使用它来解析Json
您应该创建一个处理json并使用绑定的类,以便将数据与xaml结合起来。要下载图像,请使用
HttpClient
从所有位置读取文件,然后对其进行解析,您可以这样使用
public IJsonData ParseWithJObjectParse(string json, string episodesName, string titleName)
{
var obj = JObject.Parse(json);
if (obj == null)
return null;
var items = obj.GetValue(episodesName).Select(
e => new JsonItem(((JObject)e).GetValue(titleName).ToString()));
var jsonItems = items.Cast<IJsonItem>().ToArray();
var result = new JsonData
{
Items = jsonItems
};
return result;
}
public IJsonData ParseWithJObjectParse(字符串json、字符串eposodesname、字符串titleName)
{
var obj=JObject.Parse(json);
if(obj==null)
返回null;
var items=obj.GetValue(eposodesname)。选择(
e=>newjsonim(((JObject)e.GetValue(titleName.ToString());
var jsonItems=items.Cast().ToArray();
var结果=新的JsonData
{
Items=jsonItems
};
返回结果;
}
如果您愿意,那么您也可以使用和,但我更喜欢Json.Net,因为它很简单
对于第二部分,默认情况下BitmapImage会自动缓存远程图像。最好与CreateOptions=“BackgroundCreation”结合使用,以获得最佳性能
<Image Height="100" Width="100" Margin="12,0,9,0">
<Image.Source>
<BitmapImage UriSource="{Binding ImgURL}" CreateOptions="BackgroundCreation"/>
</Image.Source>
</Image>
如果你想自己控制缓存,那么对于UriSource,我的url是json格式的,我需要在那里写什么?首先,你的链接是win 8,我为win 10 mobile(uwp)编写应用程序,据我所知,我需要下载.cs文件中的图像,而不是xamlIt文件中的图像,在那里也可以。我使用它进行解析,因为两者都基于Xaml,如果您只想缓存图像,而不想进行脱机访问,那么请使用我的方法,但如果您想在本地保存图像并脱机使用,或者类似的方法,那么请下载。希望它能消除你的疑虑。如果你澄清你的情况,那么可以提供更正确的答案