type Profile struct {
ID int `json:"id"`
FirstName string `json:"firstname"`
LastName string `json:"lastname"`
Username string `json:"username"`
Email string `json:"email"`
Phone string `json:"phone"`
Function string `json:"function"`}
我有一个问题,只为每个对象获取用户名配置文件
正如您可能看到Getprofiles()返回所有字段所以在GetprofilesApi()中我想返回json结果中的用户名字段
感谢您的任何建议!!
配置文件结构是:
const children = [8];
class PersonList extends React.Component {
state = {
trigger: 0
}
componentDidMount() {
children.push(3);
this.setState({ trigger:1 });
}
render() {
return (
<div>
{children}
</div>
);
}
}
ReactDOM.render(
<PersonList/ >,
document.getElementById('root')
);
答案 0 :(得分:2)
json:"-"
标记排除了JSON编组和解组的字段。定义一个与Person具有相同字段的新类型,并对该类型的切片进行编码(为简洁省略一些字段):
package main
import (
"encoding/json"
"fmt"
"log"
)
type Profile struct {
ID int `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
}
type ProfileSummary struct {
ID int `json:"-"`
Username string `json:"username"`
Email string `json:"-"`
}
func main() {
var profiles []Profile
profiles = append(profiles, Profile{Username: "john", Email: "john.doe@example.com"})
profiles = append(profiles, Profile{Username: "jane", Email: "jane.doe@example.com"})
summaries := make([]ProfileSummary, len(profiles))
for i, p := range profiles {
summaries[i] = ProfileSummary(p)
}
b, err := json.MarshalIndent(summaries, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}