简体   繁体   English

如何使用与Windows应用商店应用兼容的命名空间将JSON字符串转换为本机C#数组?

[英]How do i convert a JSON string to a native C# array using a namespace compatible with Windows Store Apps?

I am trying to make a windows 8 Store app that gets results from a MySQL database from a PHP page as a REST service. 我正在尝试制作一个Windows 8商店应用程序,它将来自PHP数据库的MySQL数据库的结果作为REST服务。

I'm looking for the PHP to return a JSON representation of an array of strings and have done that happily when dong the same between Javascript and PHP. 我正在寻找PHP来返回一个字符串数组的JSON表示,并且当Javascript和PHP之间相同时,我已经很愉快地完成了。 I need to take that same JSON string and use it in my C# Windows 8 store App, is there a way to take the return of that PHP page and convert it into a normal C# array, not a dictionary or more complex collection. 我需要使用相同的JSON字符串并在我的C#Windows 8商店App中使用它,有没有办法获取该PHP页面的返回并将其转换为普通的C#数组,而不是字典或更复杂的集合。

The database does have four fields so if i have to use a special object made for this i will, but I'd rather I didn't as this function doesn't require that amount of data. 数据库确实有四个字段,所以如果我必须使用一个特殊的对象,我会,但我宁愿我没有,因为这个功能不需要那么多的数据。

The PHP page is like so - $search_text is passed in via a GET: PHP页面是这样的 - $ search_text通过GET传入:

$databaseConnection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    if ($databaseConnection->connect_error)
    {
        echo "Database connection failed: $databaseConnection->connect_error";
    }
    else
    {
        $search_text = $search_text."%";
        $query = "SELECT DISTINCT street FROM gritroutes WHERE street LIKE ? LIMIT 5";
        $statement = $databaseConnection->prepare($query);
        $statement->bind_param('s', $search_text);
        $statement->execute();
        $statement->store_result();
        $statement->bind_result($street);
        $autonumber = 1;
         while ($statement->fetch())
         {          
             $resultarr[] = $street;
         }
         $statement->close();
         echo json_encode($resultarr);
    }

Just to be clear. 只是为了清楚。 I am writing a Windows Store App, the System.Web Namespace is unavailable so i can't use JavaScriptSerializer. 我正在编写一个Windows应用商店应用程序,System.Web命名空间不可用,因此我无法使用JavaScriptSerializer。

Just to add to Matthew's answer, you can deserialize using Json.NET (you can get it from NuGet ), you'd do something like: 只是为了补充Matthew的答案,你可以使用Json.NET反序列化(你可以从NuGet获得它),你可以这样做:

List<string> myStrings = JsonConvert.DeserializeObject<List<string>>(myJson);

This is in: 这是:

using Newtonsoft.Json;

Check out this article for practical example. 查看这篇文章以获取实际示例。

EDIT - I'd also like to throw in this link , since it's just awesome. 编辑 - 我也想投入这个链接 ,因为它真棒。

I hope this helps. 我希望这有帮助。

Take a look at the JavaScriptSerializer class. 看一下JavaScriptSerializer类。

string myJson = "{blablabla I'm json}";
var serializer = new JavaScriptSerializer();

var myStrings = serializer.Deserialize<List<string>>(myJson);

foreach (var str in myString)
{
    Console.WriteLine(str);
}

You could also use native Windows.Data.Json classes to do the parsing: 您还可以使用本机Windows.Data.Json类来进行解析:

string json = @"[""item1"", ""item2"", ""item3""]";
var array = JsonArray.Parse(json).Select(i => i.GetString()).ToArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM