[英]How to get a specific value from a JSON file based on another value in the JSON object in Android Studio - Java
I'm trying to put together a Mock API for my app in Android Studio using JSON, and I am trying to work out how to get a specific value from the JSON file? I'm trying to put together a Mock API for my app in Android Studio using JSON, and I am trying to work out how to get a specific value from the JSON file?
For example, I want to the get the "horoscope" string from the array "horoscopes" where the "sunsign" string equals "aries".例如,我想从数组“horoscopes”中获取“horoscope”字符串,其中“sunsign”字符串等于“aries”。
This could be super simple, but I'm not too sure where to start.这可能非常简单,但我不太确定从哪里开始。
Here's my JSON:这是我的 JSON:
"horoscopes": [
{
"horoscopeId": 1,
"sunsign": "aquarius",
"month": "january",
"horoscope": "I am the january horoscope for aquarius",
},
{
"horoscopeId": 2,
"sunsign": "pisces",
"month": "january",
"horoscope": "I am the january horoscope for pisces",
},
{
"horoscopeId": 3,
"sunsign": "aries",
"month": "january",
"horoscope": "I am the january horoscope for aries",
},
{
"horoscopeId": 4,
"sunsign": "taurus",
"month": "january",
"horoscope": "I am the january horoscope for taurus",
},
{
"horoscopeId": 5,
"sunsign": "gemini",
"month": "january",
"horoscope": "I am the january horoscope for gemini",
},
{
"horoscopeId": 6,
"sunsign": "cancer",
"month": "january",
"horoscope": "I am the january horoscope for cancer",
},
{
"horoscopeId": 7,
"sunsign": "leo",
"month": "january",
"horoscope": "I am the january horoscope for leo",
},
{
"horoscopeId": 8,
"sunsign": "virgo",
"month": "january",
"horoscope": "I am the january horoscope for virgo",
},
{
"horoscopeId": 9,
"sunsign": "libra",
"month": "january",
"horoscope": "I am the january horoscope for libra",
},
{
"horoscopeId": 10,
"sunsign": "scorpio",
"month": "january",
"horoscope": "I am the january horoscope for scorpio",
},
{
"horoscopeId": 11,
"sunsign": "sagittarius",
"month": "january",
"horoscope": "I am the january horoscope for sagittarius",
},
{
"horoscopeId": 12,
"sunsign": "capricorn",
"month": "january",
"horoscope": "I am the january horoscope for capricorn",
}
]
}
Here's what I've got so far for connecting to the JSON:这是我到目前为止连接到 JSON 的内容:
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
public class horoscope extends AppCompatActivity {
ArrayList<String> horoscope_al = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_horoscope);
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray horoscopeArray = obj.getJSONArray("horoscopes");
for (int i = 0; i < horoscopeArray.length(); i++) {
JSONObject horoscopeValues = horoscopeArray.getJSONObject(i);
horoscope_al.add(horoscopeValues.getString("horoscope"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public String loadJSONFromAsset()
{
String json_string = null;
try {
InputStream inpSt = getAssets().open("horoscope_api.json");
int s = inpSt.available();
byte[] buffer_byte = new byte[s];
inpSt.read(buffer_byte);
inpSt.close();
json_string = new String(buffer_byte, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json_string;
}
}
UPDATED CODE更新代码
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class horoscope extends AppCompatActivity {
Button back_btn;
TextView horoscope_txt;
String al_string = " ";
ArrayList<String> horoscope_al = new ArrayList<>();
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_horoscope);
horoscope_txt = (TextView)findViewById(R.id.horoscope_txt);
back_btn = (Button)findViewById(R.id.back_btn);
back_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent back_intent = new Intent(horoscope.this, menu.class);
startActivity(back_intent);
}
});
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray horoscopeArray = obj.getJSONArray("horoscopes");
List<String> horoscopesOfAries = IntStream.range(0, horoscopeArray.length())
.mapToObj(horoscopeArray::getJSONObject)
.filter(horoscopeJson -> horoscopeJson.getString("sunsign").equals("aries"))
.map(horoscopeJson -> horoscopeJson.getString("horoscope"))
.collect(Collectors.toList());
/*for (int i = 0; i < horoscopeArray.length(); i++) {
JSONObject horoscopeValues = horoscopeArray.getJSONObject(i);
horoscope_al.add(horoscopeValues.getString("horoscope"));
al_string += horoscope_al.get(i);
}*/
} catch (JSONException e) {
e.printStackTrace();
}
//horoscope_txt.setText(al_string);
}
public String loadJSONFromAsset()
{
String json_string = null;
try {
InputStream inpSt = getAssets().open("horoscope_api.json");
int s = inpSt.available();
byte[] buffer_byte = new byte[s];
inpSt.read(buffer_byte);
inpSt.close();
json_string = new String(buffer_byte, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json_string;
}
}
You can do the following:您可以执行以下操作:
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray horoscopeArray = obj.getJSONArray("horoscopes");
List<String> horoscopesOfAries = IntStream.range(0, horoscopeArray.length())
.mapToObj(horoscopeArray::getJSONObject)
.filter(horoscopeJson -> horoscopeJson.getString("sunsign").equals("aries"))
.map(horoscopeJson -> horoscopeJson.getString("horoscope"))
.collect(Collectors.toList());
Output: Output:
[I am the january horoscope for aries]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.