我有一个巨大的静态JSON文件,其中包含某个游戏中的所有冠军以及有关他们的一些信息。当前,我需要弄清楚如何找到其“关键”属性为266的冠军对象。可悲的是,我在弄清楚如何获取该对象方面遇到了一些困难。我假设我必须遍历对象,但是,通常当我遍历某些东西时,它是一个数组,但事实并非如此。
我在下面放了一个json结构的简化示例。我有一个整数266,现在我需要以某种方式使用“ key”访问该对象:“ 266”,这将是Aatrox。关于我该怎么做的任何线索?
"data": {
"Aatrox": {
"version": "8.19.1",
"id": "Aatrox",
"key": "266",
"name": "Aatrox",
"title": "the Darkin Blade"
},
"Ahri": {
"version": "8.19.1",
"id": "Ahri",
"key": "103",
"name": "Ahri",
"title": "the Nine-Tailed Fox"
}
}
答案 0 :(得分:1)
您可以使用find
Object.entries
从数据密钥获取条目find
来查找键属性等于我们期望的键的值
let obj = {"data": {"Aatrox": {"version": "8.19.1","id": "Aatrox","key": "266","name": "Aatrox","title": "the Darkin Blade"},"Ahri": {"version": "8.19.1","id": "Ahri","key": "103","name": "Ahri","title": "the Nine-Tailed Fox"}}}
let findByKey = (matchKey) => Object.entries(obj.data).find(([key,value]) => value.key === matchKey)
console.log(findByKey('266'))
答案 1 :(得分:0)
这是一个通用函数,可用于搜索任何键和值:
const obj = {"data": {"Aatrox": {"version": "8.19.1","id": "Aatrox","key": "266","name": "Aatrox","title": "the Darkin Blade"},"Ahri": {"version": "8.19.1","id": "Ahri","key": "103","name": "Ahri","title": "the Nine-Tailed Fox"}}}
const searchKeyValue = {key:"key", value:"266"}
const search = data => skv => Object.entries(data).filter(([key,value])=>value[skv.key]===skv.value)
console.log(search(obj.data)(searchKeyValue))
因此您可以通过例如searchKeyValue = {key:"id", value:"Ahri"}
或任何您喜欢的东西。
答案 2 :(得分:0)
您也可以使用JSONPath,即
return step.PromptAsync("oauth", new PromptOptions { });
假设:
public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener {
private MediaPlayer mp1, mp2, mp3;
private TextureView tv1, tv2, tv3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = findViewById(R.id.textureView1);
tv2 = findViewById(R.id.textureView2);
tv3 = findViewById(R.id.textureView3);
tv1.setSurfaceTextureListener(this);
tv2.setSurfaceTextureListener(this);
tv3.setSurfaceTextureListener(this);
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
Surface surface = new Surface(surfaceTexture);
mp1 = MediaPlayer.create(this, R.raw.a7);
mp1.setSurface(surface);
// mp1.prepareAsync(); //
mp1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp1) {
mp1.start();
}
});
Surface surface2 = new Surface(surfaceTexture);
mp2 = MediaPlayer.create(this, R.raw.a9);
mp2.setSurface(surface2);
// mp1.prepareAsync(); //
mp2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp2) {
mp2.start();
}
});
Surface surface3 = new Surface(surfaceTexture);
mp3 = MediaPlayer.create(this, R.raw.a10);
mp3.setSurface(surface3);
// mp1.prepareAsync(); //
mp3.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp3) {
mp3.start();
}
});
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
@Override
protected void onPause() {
if (mp1 != null && mp1.isPlaying()) {
mp1.pause();
}
super.onPause();
}
@Override
protected void onResume() {
if (mp1 != null) {
mp1.start();
}
super.onResume();
}
@Override
protected void onDestroy() {
if (mp1 != null) {
mp1.stop();
mp1.release();
mp1 = null;
}
super.onDestroy();
}
}