JSON to Java class

后端 未结 4 618
说谎
说谎 2021-02-13 15:22

Is there an easy way to map data from JSON to fields of my class by means of android APIs?

JSON:

{ email: \'email\', password: \'pass\' }
相关标签:
4条回答
  • 2021-02-13 15:34

    Use Jackson. Much more convenient (and if performance matters, faster) than using bundled org.json classes and custom code:

    Credentials c = new ObjectMapper().readValue(jsonString, Credentials.class);
    

    (just note that fields of Credentials need to be 'public' to be discovered; or need to have setter methods)

    0 讨论(0)
  • 2021-02-13 15:38

    I suppose you have more than one Credential u want to get...

    with the org.json package you could use a JSONArray to get various values at the same time out of one JSON file.

    0 讨论(0)
  • You could use GSON.

    0 讨论(0)
  • 2021-02-13 15:52

    Use the org.json-Package.

    JSONObject x = new JSONObject(jsonString);
    Credentials c = new Credentials();
    c.email = x.getString("email");
    c.password = x.getString("password");
    

    Its also part of the android runtime, so you dont need any external package.

    0 讨论(0)
提交回复
热议问题