我正在解析来自json的数据,如下所示。
private void dataClass() {
Intent intent = getIntent();
String Fdate = intent.getStringExtra("date");
// creating a variable for storing our string.
String url = "https://coindar.org/api/v2/events?access_token={token}&filter_date_start=" + Fdate;
// creating a variable for request queue.
RequestQueue queue = Volley.newRequestQueue(this.getApplicationContext());
// making a json object request to fetch data from API.
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@SuppressLint("NotifyDataSetChanged")
@Override
public void onResponse(JSONArray response) {
// inside on response method extracting data
// from response and passing it to array list
// on below line we are making our progress
// bar visibility to gone.
loadingPB.setVisibility(View.GONE);
try {
// extracting data from json.
for (int i = 0; i < response.length(); i++) {
JSONObject jsonObject = response.getJSONObject(i);
String caption = jsonObject.getString("caption");
String source = jsonObject.getString("source");
String sourceReliable = jsonObject.getString("source_reliable");
String important = jsonObject.getString("important");
int coinID = jsonObject.getInt("coin_id");
String priceChange = jsonObject.getString("coin_price_changes");
int tagNumber = jsonObject.getInt("tags");
// adding all data to our array list.
calendarEventModalArrayList.add(new CalendarEventModal(caption, source, sourceReliable, important, coinID, priceChange, tagNumber));
}
// notifying adapter on data change.
calendarEventRVAdapter.notifyDataSetChanged();
} catch (JSONException e) {
// handling json exception.
e.printStackTrace();
Toast.makeText(CalendarEvent.this, "Something went amiss. Please try again later", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// displaying error response when received any error.
Toast.makeText(CalendarEvent.this, "Something wentaaaamiss. Please try again later", Toast.LENGTH_SHORT).show();
}
}) {
};
// calling a method to add our
// json object request to our queue.
queue.add(jsonArrayRequest);
}
json对象是这样的,
{
"caption": "Landing Page Launch",
"source": "https://coindar.org/en/event/bzx-protocol-bzrx-landing-page-launch-56269",
"source_reliable": "true",
"important": "false",
"date_public": "2021-11-25 15:32",
"date_start": "2021-12-08",
"date_end": "",
"coin_id": "8630",
"coin_price_changes": "7.24",
"tags": "15"
},
我正在获取硬币Id,现在从这个硬币Id中,我想扫描另一个json文件,寻找特定的Id,并从中获取名称或符号,
另一个json包含,
{
"id": "1",
"name": "Bitcoin",
"symbol": "BTC",
"image_32": "https://coindar.org/images/coins/bitcoin/32x32.png",
"image_64": "https://coindar.org/images/coins/bitcoin/64x64.png"
},
{
"id": "2",
"name": "Ethereum",
"symbol": "ETH",
"image_32": "https://coindar.org/images/coins/ethereum/32x32.png",
"image_64": "https://coindar.org/images/coins/ethereum/64x64.png"
},
this continues for over 11,000 coins :(.
我的bindviewholder方法,
public void onBindViewHolder(@NonNull CalendarEventRVAdapter.CalendarEventViewHolder holder, int position) {
// on below line we are setting data to our item of
// recycler view and all its views.
CalendarEventModal modal = calendarEventModal.get(position);
holder.headingTV.setText(modal.getCaption());
holder.priceChangeTV.setText(modal.getPriceChange() + "% from event announcement");
if(modal.getSourceReliable().matches("true")) {
holder.sourceReTV.setText("Source: Reliable");
}
if(modal.getSourceReliable().matches("false")) {
holder.sourceReTV.setText("Source: Not-reliable");
}
}
现在我的问题是,如何在bindviewholder类上从第一个json获取硬币ID,然后扫描第二个json以获取该ID并获取硬币的名称,然后执行holder.settext
之类的操作。
现在我解析第二个json如下,
private void dataClass2() {
// creating a variable for storing our string.
String url = "https://coindar.org/api/v2/coins?access_token={token}";
// creating a variable for request queue.
RequestQueue queue = Volley.newRequestQueue(this.getApplicationContext());
// making a json object request to fetch data from API.
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@SuppressLint("NotifyDataSetChanged")
@Override
public void onResponse(JSONArray response) {
// inside on response method extracting data
// from response and passing it to array list
// on below line we are making our progress
// bar visibility to gone.
try {
// extracting data from json.
for (int i = 0; i < response.length(); i++) {
JSONObject jsonObject = response.getJSONObject(i);
int cdID = jsonObject.getInt("id");
String cdnName = jsonObject.getString("symbol");
String cdsImage = jsonObject.getString("image_64");
// adding all data to our array list.
calendarCoinModalArrayList.add(new CalendarCoinModal(cdID, cdnName, cdsImage));
}
// notifying adapter on data change.
} catch (JSONException e) {
// handling json exception.
e.printStackTrace();
Toast.makeText(Calendar.this, "Something went amiss. Please try again later", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// displaying error response when received any error.
Toast.makeText(Calendar.this, "Something wentaaaamiss. Please try again later", Toast.LENGTH_SHORT).show();
}
}) {
};
// calling a method to add our
// json object request to our queue.
queue.add(jsonArrayRequest);
}
另外,在解析第二个json时
private ArrayList<CalendarCoinModal> calendarCoinModalArrayList;
private CalendarCoinRVAdapter calendarCoinRVAdapter;
}
calendarCoinModalArrayList = new ArrayList<>();
// initializing our adapter class.
calendarCoinRVAdapter = new CalendarCoinRVAdapter(calendarCoinModalArrayList, this);
// calling get data method to get data from API.
dataClass();
我们将不胜感激。
在使用settext
设置测试之前,需要加载关于第二个JSON内容的信息。尝试从文件中读取json信息。
public String loadJSONFromResource() {
String json = null;
try {
InputStream is = getActivity().getResources().openRawResource(R.raw.coindata);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
要基于id
属性进行搜索,假设您使用了ArrayList
。
CalendarCoinModal obj;
int searchId = somevalue;
for(CalendarCoinModal currObj : calendarCoinModalArrayList) {
if(currObj.id == somevalue) {
obj = currObj;
break;
}
}
我想从这个json数据中提取id值。我尝试了很多方法,但我不知道我的代码出了什么问题,我也不想把id存储到数组中
问题内容: 我有这个数组: 是否可以通过知道名称来获取值或特定元素? 像这样的东西: 要么 问题答案: 通常通过数字索引访问数组,因此在您的示例中。如果您知道每个对象的属性都是唯一的,则可以将它们存储在对象而不是数组中,如下所示: 如果您实际上想要一个像您的帖子中那样的对象数组,则可以遍历该数组并在找到具有对象具有所需属性的元素时返回:
问题内容: 如何使用Jackson从JSON树中接收节点名称?JSON文件看起来像这样: 我有 并且需要类似的东西 谢谢。 问题答案: 此答案适用于2+之前的Jackson版本(最初写为1.8)。请参阅@SupunSameera的答案,以获取与较新版本的Jackson兼容的版本。 “节点名称”的JSON术语是“键”。由于 不包含键,因此您需要进行不同的迭代: 如果 只 需要查看键,则可以使用以下方
我试图从以下*链接* http://21.26.54.26/swapi/api/data/json http://21.26.54.26/swapi/api/data/xml中获取数据 我尝试了许多方法,但最后,当它通过这一行时总是给我错误 这是错误**long**在该行中断时抛出的错误**long** 01-11 10:04:47.487 654 1-7024/pa.com.tropigas.p
我需要保存通过json发送的响应代码,例如404: 我尝试使用以下代码: 但是laravel告诉我这个错误: 非静态方法Symfony\Component\HttpFoundation\Response::getStatusCode()不应静态调用