我如何调整此代码,使其只有一个数组,但您可以将其转换为一个对象(以便您可以在前端将其用作 JObject)


How can I adjust this code so it only has one array but you make it into an object (so you can work with it as a JObject in the front end)?

有点

像PHP新手,无法让它按照我想要的方式工作。我想将数据作为对象发送,以便我可以在前端将其作为 JObject 使用。我目前有两个数组(它在代码中工作,我可以将其作为 JObject 接收,但它给了我两个列表,两个数组),我当然只想要一个。

这是代码:

        $contacts = array();
        while ($row = mysqli_fetch_array($stmt))
        {
            $contact = array("ID" => $row['ProduktID'],
                             "Name" => $row['ProduktNamn'],
                         "Number" => $row['ProduktPris']);
            array_push($contacts, $contact);
        }
        echo json_encode(array('results' => $contacts, JSON_PRETTY_PRINT));

}

该问题与 C# 有关。所以解决方案是这样的:

var result = await httpClientRequest.GetAsync ("localhost/GetStuff.php"); 
var resultString = await result.Content.ReadAsStringAsync (); 
var jsonResult = JObject.Parse (resultString);
foreach (int element in jsonResult['results']) {
    ourList.Add (new Contact ((JArray) element);
    System.Diagnostics.Debug.WriteLine (element['Name'] + " is the value.");
}