在关联数组上使用JArray.Parse(json)

时间:2018-10-05 19:56:31

标签: c# json.net

我整理了以下代码from this answer:

import React from 'react';
import PortfolioForm from './PortfolioForm';
import Section from './Section';

class SubmitProject extends React.Component {

    render() {
        return(
                <React.Fragment>
                    <h1>Submit Project</h1>
                    <h2>Enter Project Data</h2>
                    <ul className="section">
                        {Object.keys(this.state.sections).map(key => <Section key={key} details={this.state.sections[key]}/>)}
                    </ul>
                    <PortfolioForm addSection={this.addSection} />
                </React.Fragment>   

        )
    }
}

export default SubmitProject;

我要解析的JSON示例如下:

/**
 * Create a new command instance.
 *
 * @return void
 */
public function __construct()
{
    $this->signature = sprintf(
        "myapp:command {--i|id %s}",
        __("This is some help text for the ID")
    );
    parent::__construct();
}

我引用的答案涉及解析键=>值对,如何解析关联数组?

1 个答案:

答案 0 :(得分:2)

JSON没有“关联数组”。它具有数组和对象。

这里拥有的是一组对象。因此,$locations = array(); $locas = array(); $infoDialog = array(); if (is_numeric($_POST['searchid'])) { $service_id = $_POST['searchid']; $query = "SELECT * FROM tblemployees WHERE FIND_IN_SET($service_id, service)"; if (!$result = mysqli_query($conn, $query)) { exit(mysqli_error($conn)); } if (mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_array($result)) { // username is already exist $print_info = null; $latitude = $row['ltd']; $longitude = $row['lotd']; $person_name = $row['fullname']; $person_id = $row['id']; $person_contact = $row['contact']; $person_address = $row['address']; $locations[] = array( 'name' => $person_name, 'lat' => $latitude, 'lng' => $longitude, 'lnk' => $person_id ); $locas[] = "['" . $person_name . "', " . $latitude . ", " . $longitude . ", " . $person_id . "]"; $print_info .= '<div class="info_content">'; $print_info .= '<h3>' . $person_name . '</h3>'; $print_info .= '<h4>Contact: ' . $person_contact . '</h4>'; $print_info .= '<p>' . $person_address . '</p>'; $print_info .= '</div>'; $infoDialog[] = "['" . $print_info . "']"; } } $json_response = json_encode($locas); $json_info = json_encode($infoDialog); echo $json_response . "==============" . $json_info; } 会给您一个JArray.Parse,并且其中的每个项目本身就是一个JArray

JObject