JSON response Variable missing during compile time

apigee json extract variable
moshi convert json to object
rest api response best practices
rest api design example

I am setting up the recyclerview to show some data from the server and this is how my jsonpojo looks alike :

public class JSON {

    public class MainCard {
        @SerializedName("Cards")
        public List<Cards> cards;
    }

    public class Cards {
        @SerializedName("Title")
        public String title;
        @SerializedName("Items")
        public List<ItemData> items;
    }

    public class ItemData {
        @SerializedName("Name")
        public String name;
        @SerializedName("Thumb")
        public String thumb;
        @SerializedName("Link")
        public String link;
    }
}

and here is the adapter :

public class API_Adpater extends RecyclerView.Adapter<API_Adpater.CardsHolder> {
    private List<JSON.ItemData> mlist;
    private List<JSON.Cards> mCards;
    private Context mcontext;

    public API_Adpater(List<JSON.ItemData> mlists, List<JSON.Cards> titles, Context context) {
        this.mlist = mlists;
        this.mcontext = context;
        this.mCards = titles;
    }

    @NonNull
    @Override
    public CardsHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.singleitems,viewGroup,false);
        return new CardsHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CardsHolder cardsHolder, int i) {
        final JSON.ItemData singleitem = mlist.get(i);
        final JSON.Cards Title = mCards.get(i);
        cardsHolder.textView.setText(Title.title);
        cardsHolder.textView2.setText(singleitem.name);
        cardsHolder.url = singleitem.thumb;
        Glide.with(this.mcontext).load(cardsHolder.url).into(cardsHolder.imageView);
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    class CardsHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;
        TextView textView2;
        String url;
        CardsHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            textView = itemView.findViewById(R.id.textView);
            textView2 = itemView.findViewById(R.id.textView2);
        }
    }
}

and this is the mainactivity :

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(APIService.url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        APIService service = retrofit.create(APIService.class);
        Call<JSON.MainCard> call = service.getCards();

        call.enqueue(new Callback<JSON.MainCard>() {
            @Override
            public void onResponse(Call<JSON.MainCard> call, Response<JSON.MainCard> response) {
                if (response.isSuccessful()) {
                    JSON.MainCard mainCard = response.body();
                    if (mainCard != null && mainCard.cards !=null) {
                        List<JSON.ItemData> ru = mainCard.cards.items;
// Here on Above Line it can't get the mainCard.Cards.items; it is not showing the `.items` in the code;
                        setupRV(ru);
                    }
                } else {
                    Toast.makeText(MainActivity.this, "Reposnce Error", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<JSON.MainCard> call, Throwable t) {
                Toast.makeText(MainActivity.this, "Check Internet Connectivity", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setupRV(List<JSON.ItemData> list) {
        RecyclerView recyclerView = findViewById(R.id.rv);
        LinearLayoutManager lm = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(lm);
        recyclerView.setAdapter(new API_Adpater(list,this));
    }
} 

On response of retrofit, I need to set the line like List<JSON.ItemData> ru = mainCard.cards.items; but it is not working as the codeeditor is not getting the .items variable where is the error? Can't set the recylcer view.

This is how my json looks alike :

{
  "Cards": [
    {
      "Title": "Title",
      "Items": [
        {
          "Name": "Name",
          "Thumb": "Thumb",
          "Link": "link"
        }
      ]
    },
    {
      "Title": "Title",
      "Items": [
        {
          "Name": "Name",
          "Thumb": "Thumb",
          "Link": "link"
        }
      ]
    }
  ]
} 

And this the error it can't find the items from the maincards.cards

I need to set the image from the url and name and title in the recycler view.


You're accessing items directly on list in this line

List<JSON.ItemData> ru = mainCard.cards.items; 

here cards is a list, that's why you can't access items directly Get the object of Cards and then use it

List<JSON.ItemData> ru  = mainCard.cards.get(index).items;

Based on your requirements you need to iterate over

if (mainCard != null && mainCard.cards !=null) {
        List<JSON.ItemData> ru = new ArrayList();
        for(Cards itemCard : mainCard.cards)
        {
            ru.addAll(itemCard.items);
        }
                setupRV(ru);
}

Recall that the response variable is used in the <Source> element of the Missing input payload, The input payload (JSON, XML) is empty. JSON is the name of a class located in the com.adobe.serialization.json.JSO package that is provided with the API. The problem should pop-up at compile-time, not at runtime. Don't you get any errors during the compilation?


Try this..

 List<MoviesApi.ItemData> ru = mainCard.cards.getItems();

In this article, we'll look at how to design REST APIs to be easy to Let's take a look at an example API that accepts JSON payloads. We also need ways to paginate data so that we only return a few results at a time. In the code above, we have the req.query variable to get the query parameters. TypeScript Version: 2.3.0 Expected behavior: JSON.parse() should return Object, as stated in MDN. Actual behavior: JSON.parse() returns any, essentially getting rid of the useful compile-time type checks. I realized about this while work


Make separate class for each model as below.

-----------------------------------com.example.Card.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Card {

@SerializedName("Title")
@Expose
public String title;
@SerializedName("Items")
@Expose
public List<Item> items = null;

}
-----------------------------------com.example.Item.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Item {

@SerializedName("Name")
@Expose
public String name;
@SerializedName("Thumb")
@Expose
public String thumb;
@SerializedName("Link")
@Expose
public String link;

}
-----------------------------------com.example.MainCard.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class MainCard {

@SerializedName("Cards")
@Expose
public List<Card> cards = null;

}

To create these pojos I used this site http://www.jsonschema2pojo.org/

Angular 6 — Compile Time vs Run Time Environments For this in angular 6, we have environment files out of the box. Here you can see we have same json for dev and prod environments, but with different after import, you can consume your static variable where required. Write the first response  w.WriteHeaer(http.StatusOK) writes the HTTP response code 200, indicating that the requested resource (the JSON response in our case) has been successfully fetched and is transmitted in the message body. w.Header().Set("Content-Type", "application/json") indicates the return type of the requested resource.


Moshi is a modern JSON library for Android and Java from Square. In this simple example, the JSON object key names exactly match the throw JsonDataException("Missing required field") Now for every Kotlin class for which you want to generate a JsonAdapter implementation at compile time, add  No hand-written boilerplate is needed, and typos when accessing JSON fields are caught at compile-time. The downside with code generation is that it requires some initial setup. Also, the generated source files might produce visual clutter in your project navigator.


For example, the SQLITE_THREADSAFE=0 option is only usable by applications If this option is missing or false, then SQLite substitutes its own home-grown If the target byte order cannot be determined at compile-time, then SQLite falls When this option is defined in the amalgamation, the JSON SQL functions are  You can using JSON Schema and use a JSON schema validator without TypeScript, or even in conjunction with, which is typically the preferred way for distributed applications, since TypeScript is a compile time library.


The recommended alternative is to use messageformat as a compile-time tool. Webpack loaders for JSON, .properties, gettext PO, and YAML files Using messageformat-loader as an example, these enable a JavaScript API that looks like this: along with errors from missing properties for messages using variables. At the root of the JSON document, there needs to be either a JSON object or a JSON array. In JavaScript and in many other programming languages an object is denoted by curly braces; “ { }”. JSON uses the same notation for denoting the objects in the JSON file. In JSON files, objects only contain the properties that they have. JSON can use