[英]Read JSON from file UWP (C#)
我剛開始創建通用應用
我在.txt文件中有JSON。 xaml中的圖像和文本框
我需要從.txt讀取JSON並將文本從JSON解析到TextBox。 對於圖像,我需要通過url(在JSON中)下載
我的JSON
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);
}
在Xamarin.Android中,我有Koush插件,用於下載和緩存圖像
如何將信息從Json解析到UWP中的TextBox,以及如何下載圖像並緩存它?
您可以使用Json.NET解析json。
您應該創建一個處理json並使用綁定的類,以便將數據與xaml合並。 為了下載圖像使用HttpClient
要從所有位置讀取文件, 請檢查此鏈接 ,然后對其進行解析,您可以使用Json.Net程序包,如下所示
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;
}
如果願意,還可以使用Simple.Json和Microsoft.Json,但我更喜歡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>
如果您想控制自己的緩存, 請檢查此鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.