About displaying Json data in Jackson on Android Studio

Asked 2 years ago, Updated 2 years ago, 110 views

public class MainActivity extensions AppCompatActivity{
    @ Override
    protected void onCreate (Bundle savedInstanceState) {


        ObjectMapper mapper = new ObjectMapper();
        try{

            JackSon response=mapper.readValue(


             new URL ("http://express.heartrails.com/api/json?method=getAreas"),
                    JackSon.class);
            String m=response.get("response").get(0).get("area").asText();

            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

            TextView textView= (TextView) findViewById (R.id.text_view);

            textView.setText(String.valueOf(m));

        } catch(IOExceptione){
            e.printStackTrace();
        }

    }
}

In the above code, the response.get get has the error Cannot resolve method 'get(java.lang.String)'.

@Data
public class JackSon{
    private area response;

    @ Data
    public class area {
        private String [ ] area;
    }
}

How can I display this code?

[
  {
    "a": "xxx",
    "b" —yyy
  },
  {
    "a": "xxxx",
    "b" —yyyy
  },
  {
    "a": "xx",
    "b" —yy
  }
]

How can I change it if I want to display JSON data like this instead of the above URL?

java android-studio json jackson

2022-09-30 19:25

2 Answers

JavaBean class JackSon:

import com.fasterxml.jackson.announcement.JsonProperty;

import java.util.List;

public class JackSon{

    @ JsonProperty ("response")
    private response;

    public void setResponse(Response response) {
        this.response=response;
    }

    public Response getResponse(){
        return response;
    }


    public static class Response {

        @ JsonProperty ("area")
        private List <String > area;

        public void setArea(List<String>area){
            This.area=area;
        }

        publicList<String>getArea(){
            return area;
        }

    }
}
public class MainActivity extensions AppCompatActivity{
    @ Override
    protected void onCreate (Bundle savedInstanceState) {
        ObjectMapper mapper = new ObjectMapper();
        JackSon response=null;
        try{
            response=mapper.readValue(new URL("http://express.heartrails.com/api/json?method=getAreas"), JackSon.class);

        final List<String>area=response.getResponse().getArea();
        final Strings=area.toString();
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

            TextView textView= (TextView) findViewById (R.id.text_view);

            textView.setText(String.valueOf(m));
        } catch(IOExceptione){
            e.printStackTrace();
        }
    }
}


2022-09-30 19:25

In the above code, the response.get get has the error Cannot resolve method 'get(java.lang.String)'.

I think @Data is Lombok annotation, so getter is generated automatically.Therefore, the code for retrieving area data is

response.getResponse().getArea();

is the case.
(It's not directly related to the question, but you don't need to define the Area class as a nonstatic internal class, and generally you shouldn't.)

How can I change it if I want to display JSON data like this instead of the above URL?

Conversion of JSON with the top level [] depends on whether you convert it to an array or to a list.

@Data
US>class MyData{
    private String a;
    private intb;
}

For classes defined as :

a) When converting to an array

MyData[]myDataArray=mapper.readValue(...,MyData[].class);

b) To convert to a list

List<MyData>myDataList=mapper.readValue(..., new TypeReference<List<MyData>>(){});

(However, even if you want to convert it to a list, it is faster to convert it to an array using the former method and then to a list instead of using TypeReference.See the reference link below.)

Note:


2022-09-30 19:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.