json - 仅访问 Json 值的一部分
问题描述
我想参加 JSON 元素的一部分。我有 JSON 格式
{
"reservation":[
{
"resa":"1902211200-1802211330"
},
{
"resa":"1902221130-1902221230"
}
]
}
对于每个预订,我只想访问最后的 4 个值 - 所以这里第一个 resa为1330 ,第二个为1230
<View>
<FlatList
data= {this.state.JsonList}
renderItem={({item}) => <Text>{item.resa}.substring(17, 20)</Text>}
keyExtractor={(item, index) => index.toString()} />
</View>
substring 不起作用并显示单词 substring 但我找不到如何做。你能告诉我如何只显示 JSON 值的一部分吗?
解决方案
{}
如果您希望它正确计算子字符串,您应该包含完整的函数。之外的任何内容{}
都将呈现为字符串。
<Text>{item.resa.substring(17, 21)}</Text>
您还需要更改它,以便在字符串中包含最后一个字符。否则,如果你使用substring(17, 20)
那么你只会得到133
而不是1330
这样使用substring(17, 21)
let item = { resa: "1902211200-1802211330" }
console.log(item.resa.substring(17,20)) // 133
console.log(item.resa.substring(17,21)) // 1330
console.log(item.resa.substring(item.resa.length-4)) // you could use the length to calculate the substring
console.log(item.resa.slice(-4)) // you could always use slice
推荐阅读
- react-native - 带有子菜单的 createStackNavigator
- python - 计算列中每一行的 len 并且只取一个尊重条件 Pandas Python
- javascript - Selenium Javascript Executor Click() 在实际点击页面之前搜索页面上的元素
- arrays - Powershell从foreach循环中的数组替换字符串
- c# - 2dArray 到 Json 字符串
- applepay - 设备库标识符是否随推送令牌而变化?
- php - 来自 WhereIn 的访问关系
- mongodb - 连接到另一个嵌套集合的 MongoDB 集合中的全文搜索
- android-recyclerview - 如何在 RecyclerView 中处理无效的 RealmList
- typescript - 如何在 Typescript 中检查二进制类型别名