如何基于Android Studio-Java中JSON对象中的另一个值从JSON文件中获取特定值
我正在尝试使用JSON在Android Studio中为我的应用程序构建一个模拟API,我正在尝试解决如何从JSON文件中获取特定值 例如,我想从数组“horoscopes”中获取“horoscope”字符串,其中“sunsign”字符串等于“aries” 这可能非常简单,但我不太确定从哪里开始 以下是我的JSON:如何基于Android Studio-Java中JSON对象中的另一个值从JSON文件中获取特定值,java,android,json,api,Java,Android,Json,Api,我正在尝试使用JSON在Android Studio中为我的应用程序构建一个模拟API,我正在尝试解决如何从JSON文件中获取特定值 例如,我想从数组“horoscopes”中获取“horoscope”字符串,其中“sunsign”字符串等于“aries” 这可能非常简单,但我不太确定从哪里开始 以下是我的JSON: "horoscopes": [ { "horoscopeId": 1, "sunsign&qu
"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",
}
]
}
到目前为止,我已经获得了连接到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;
}
}
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
公共类星象活动{
ArrayList占星术=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动和星座);
试一试{
JSONObject obj=新的JSONObject(loadJSONFromAsset());
占星术array=obj.getJSONArray(“占星术”);
对于(int i=0;i
更新代码
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;
}
}
导入androidx.annotation.RequiresApi;
导入androidx.appcompat.app.appcompat活动;
导入android.content.Intent;
导入android.os.Build;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.stream.collector;
导入java.util.stream.IntStream;
公共类星象活动{
按钮返回_btn;
文字视图星座图;
字符串al_String=“”;
ArrayList占星术=新的ArrayList();
@RequiresApi(api=Build.VERSION\u code.N)
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动和星座);
占星术txt=(TextView)findViewById(R.id.horoscope_txt);
back\u btn=(按钮)findViewById(R.id.back\u btn);
back_btn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意向返回\意向=新意向(占星术、本、菜单、类);
起始触觉(背向意图);
}
});
试一试{
JSONObject obj=新的JSONObject(loadJSONFromAsset());
占星术array=obj.getJSONArray(“占星术”);
列出horoscopesOfAries=IntStream.range(0,horoscopeArray.length())
.mapToObj(占星术array::getJSONObject)
.filter(horoscopeJson->horoscopeJson.getString(“sunsign”).equals(“aries”))
.map(horoscopeJson->horoscopeJson.getString(“占星术”))
.collect(Collectors.toList());
/*对于(int i=0;i
您可以执行以下操作:
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());
非常感谢你!唯一的问题是“mapToObj(horoscoparray::getJSONObject)”位抛出了一个错误,说“Unhandled exception:org.json.JSONException”,你知道是什么原因吗?@ells99我认为json缺少一个{'一开始。添加它后,我得到了输出。你可以用这个工具确定JSON-啊,是的,谢谢你,我认为这是我的垃圾复制和粘贴:/我刚刚用这个工具检查了我的JSON,它都是有效的,但错误仍然存在,所以我不太确定还有什么其他原因it@ells99它详细说明了错误吗?这是我的ine只是OroscopeArray.getJSONObject(i)对于arrayAh中的每个json,它说“如果存在并且是JSONObject,则返回索引处的值。如果该值不存在或者不是JSONObject,则抛出org.json.JSONException。”但它已经在尝试捕获中了?
[I am the january horoscope for aries]