Home > Back-end >  Getting id from one json and finding name of that code in json
Getting id from one json and finding name of that code in json

Time:12-10

I am parsing data from json, like this.

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 one rrorResponse(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);

    }

and json object is something like this,

{
    "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"
  },

I am getting the coin Id, and now from that coin ID, i want to scan another json file, for that specific id, and get name or symbol from there,

the other json contains,

{
    "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 :(.

My bindviewholder method,

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");
        }
}

Now my question is, how I can on bindviewholder class get the coin ID from first json, and then scan second json for that id and get name of coin and then do holder.settext stuff.

Now i am parsing second json as below,

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 one rrorResponse(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);

    }

Also, while parsing second 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();

Help would be appreciated.

CodePudding user response:

Before setting the test using settext, you need to load the information about the second JSON content. Try this to read json information from the file.

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;
}

To search based on the id property, Assuming you used an ArrayList.

CalendarCoinModal obj;
int searchId = somevalue;
for(CalendarCoinModal currObj : calendarCoinModalArrayList) {
    if(currObj.id == somevalue) {
        obj = currObj;
        break;
    }
}
  • Related