Can't parse Json String with Moshi











up vote
2
down vote

favorite












I'm trying to parse a Json response with Moshi, the problem that I have is that the value of the key is a Json wrap on a String:



{"mobile_accounts_account_showProperties":"[{"name": "current"},{"name": "available"}]"}


This is my class



@Json(name = "mobile_accounts_account_showProperties")
private List<PropertyConfig> showProperties;


I try removing the ("") with a replace(""[", "[") and a replace("\", "") before parsing but is not an option because that remove some other quotes that I do need. I try to use JsonAdapter but I can't make that work. The JsonAdapter is not getting call.



public class PropertyConfigJsonAdapter {

public PropertyConfigJsonAdapter() {
}

@ToJson
String toJson(List<PropertyConfig> card) {
return "";
}

@FromJson
List<PropertyConfig> fromJson(String object) {

return new ArrayList<>();
}


I try this to see if the JsonAdapter was getting call but it never call the "fromJson" method. Here is how I call the adapter:



MoshiConverterFactory.create(new Moshi.Builder().add(new PropertyConfigJsonAdapter()).build())









share|improve this question


























    up vote
    2
    down vote

    favorite












    I'm trying to parse a Json response with Moshi, the problem that I have is that the value of the key is a Json wrap on a String:



    {"mobile_accounts_account_showProperties":"[{"name": "current"},{"name": "available"}]"}


    This is my class



    @Json(name = "mobile_accounts_account_showProperties")
    private List<PropertyConfig> showProperties;


    I try removing the ("") with a replace(""[", "[") and a replace("\", "") before parsing but is not an option because that remove some other quotes that I do need. I try to use JsonAdapter but I can't make that work. The JsonAdapter is not getting call.



    public class PropertyConfigJsonAdapter {

    public PropertyConfigJsonAdapter() {
    }

    @ToJson
    String toJson(List<PropertyConfig> card) {
    return "";
    }

    @FromJson
    List<PropertyConfig> fromJson(String object) {

    return new ArrayList<>();
    }


    I try this to see if the JsonAdapter was getting call but it never call the "fromJson" method. Here is how I call the adapter:



    MoshiConverterFactory.create(new Moshi.Builder().add(new PropertyConfigJsonAdapter()).build())









    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm trying to parse a Json response with Moshi, the problem that I have is that the value of the key is a Json wrap on a String:



      {"mobile_accounts_account_showProperties":"[{"name": "current"},{"name": "available"}]"}


      This is my class



      @Json(name = "mobile_accounts_account_showProperties")
      private List<PropertyConfig> showProperties;


      I try removing the ("") with a replace(""[", "[") and a replace("\", "") before parsing but is not an option because that remove some other quotes that I do need. I try to use JsonAdapter but I can't make that work. The JsonAdapter is not getting call.



      public class PropertyConfigJsonAdapter {

      public PropertyConfigJsonAdapter() {
      }

      @ToJson
      String toJson(List<PropertyConfig> card) {
      return "";
      }

      @FromJson
      List<PropertyConfig> fromJson(String object) {

      return new ArrayList<>();
      }


      I try this to see if the JsonAdapter was getting call but it never call the "fromJson" method. Here is how I call the adapter:



      MoshiConverterFactory.create(new Moshi.Builder().add(new PropertyConfigJsonAdapter()).build())









      share|improve this question













      I'm trying to parse a Json response with Moshi, the problem that I have is that the value of the key is a Json wrap on a String:



      {"mobile_accounts_account_showProperties":"[{"name": "current"},{"name": "available"}]"}


      This is my class



      @Json(name = "mobile_accounts_account_showProperties")
      private List<PropertyConfig> showProperties;


      I try removing the ("") with a replace(""[", "[") and a replace("\", "") before parsing but is not an option because that remove some other quotes that I do need. I try to use JsonAdapter but I can't make that work. The JsonAdapter is not getting call.



      public class PropertyConfigJsonAdapter {

      public PropertyConfigJsonAdapter() {
      }

      @ToJson
      String toJson(List<PropertyConfig> card) {
      return "";
      }

      @FromJson
      List<PropertyConfig> fromJson(String object) {

      return new ArrayList<>();
      }


      I try this to see if the JsonAdapter was getting call but it never call the "fromJson" method. Here is how I call the adapter:



      MoshiConverterFactory.create(new Moshi.Builder().add(new PropertyConfigJsonAdapter()).build())






      jsonparser moshi






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 14:28









      Ezequiel Di Sisto

      111




      111
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          The easiest way is going to be to read in the value as a string and decode it from there. You can make this transparent to your application code with a JsonAdapter, like the ShowPropertiesContainer.ADAPTER below.



          public final class ShowPropertiesContainer {
          public final List<PropertyConfig> showProperties;

          public static final Object ADAPTER = new Object() {
          @FromJson public ShowPropertiesContainer fromJson(JsonReader reader,
          JsonAdapter<ShowPropertiesContainer> fooAdapter,
          JsonAdapter<List<PropertyConfig>> propertyConfigListAdapter) throws IOException {
          ShowPropertiesContainer showPropertiesContainer = fooAdapter.fromJson(reader);
          return new ShowPropertiesContainer(propertyConfigListAdapter.fromJson(
          showPropertiesContainer.mobile_accounts_account_showProperties),
          showPropertiesContainer.mobile_accounts_account_showProperties);
          }
          };

          final String mobile_accounts_account_showProperties;

          ShowPropertiesContainer(List<PropertyConfig> showProperties,
          String mobile_accounts_account_showProperties) {
          this.showProperties = showProperties;
          this.mobile_accounts_account_showProperties = mobile_accounts_account_showProperties;
          }
          }

          public final class PropertyConfig {
          public final String name;

          PropertyConfig(String name) {
          this.name = name;
          }
          }

          @Test public void showPropertiesContainer() throws IOException {
          Moshi moshi = new Moshi.Builder()
          .add(ShowPropertiesContainer.ADAPTER)
          .build();
          JsonAdapter<ShowPropertiesContainer> adapter = moshi.adapter(ShowPropertiesContainer.class);
          String encoded =
          "{"mobile_accounts_account_showProperties":"[{\"name\": \"current\"},"
          + "{\"name\": \"available\"}]"}";
          ShowPropertiesContainer showPropertiesContainer = adapter.fromJson(encoded);
          }





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414280%2fcant-parse-json-string-with-moshi%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            The easiest way is going to be to read in the value as a string and decode it from there. You can make this transparent to your application code with a JsonAdapter, like the ShowPropertiesContainer.ADAPTER below.



            public final class ShowPropertiesContainer {
            public final List<PropertyConfig> showProperties;

            public static final Object ADAPTER = new Object() {
            @FromJson public ShowPropertiesContainer fromJson(JsonReader reader,
            JsonAdapter<ShowPropertiesContainer> fooAdapter,
            JsonAdapter<List<PropertyConfig>> propertyConfigListAdapter) throws IOException {
            ShowPropertiesContainer showPropertiesContainer = fooAdapter.fromJson(reader);
            return new ShowPropertiesContainer(propertyConfigListAdapter.fromJson(
            showPropertiesContainer.mobile_accounts_account_showProperties),
            showPropertiesContainer.mobile_accounts_account_showProperties);
            }
            };

            final String mobile_accounts_account_showProperties;

            ShowPropertiesContainer(List<PropertyConfig> showProperties,
            String mobile_accounts_account_showProperties) {
            this.showProperties = showProperties;
            this.mobile_accounts_account_showProperties = mobile_accounts_account_showProperties;
            }
            }

            public final class PropertyConfig {
            public final String name;

            PropertyConfig(String name) {
            this.name = name;
            }
            }

            @Test public void showPropertiesContainer() throws IOException {
            Moshi moshi = new Moshi.Builder()
            .add(ShowPropertiesContainer.ADAPTER)
            .build();
            JsonAdapter<ShowPropertiesContainer> adapter = moshi.adapter(ShowPropertiesContainer.class);
            String encoded =
            "{"mobile_accounts_account_showProperties":"[{\"name\": \"current\"},"
            + "{\"name\": \"available\"}]"}";
            ShowPropertiesContainer showPropertiesContainer = adapter.fromJson(encoded);
            }





            share|improve this answer

























              up vote
              0
              down vote













              The easiest way is going to be to read in the value as a string and decode it from there. You can make this transparent to your application code with a JsonAdapter, like the ShowPropertiesContainer.ADAPTER below.



              public final class ShowPropertiesContainer {
              public final List<PropertyConfig> showProperties;

              public static final Object ADAPTER = new Object() {
              @FromJson public ShowPropertiesContainer fromJson(JsonReader reader,
              JsonAdapter<ShowPropertiesContainer> fooAdapter,
              JsonAdapter<List<PropertyConfig>> propertyConfigListAdapter) throws IOException {
              ShowPropertiesContainer showPropertiesContainer = fooAdapter.fromJson(reader);
              return new ShowPropertiesContainer(propertyConfigListAdapter.fromJson(
              showPropertiesContainer.mobile_accounts_account_showProperties),
              showPropertiesContainer.mobile_accounts_account_showProperties);
              }
              };

              final String mobile_accounts_account_showProperties;

              ShowPropertiesContainer(List<PropertyConfig> showProperties,
              String mobile_accounts_account_showProperties) {
              this.showProperties = showProperties;
              this.mobile_accounts_account_showProperties = mobile_accounts_account_showProperties;
              }
              }

              public final class PropertyConfig {
              public final String name;

              PropertyConfig(String name) {
              this.name = name;
              }
              }

              @Test public void showPropertiesContainer() throws IOException {
              Moshi moshi = new Moshi.Builder()
              .add(ShowPropertiesContainer.ADAPTER)
              .build();
              JsonAdapter<ShowPropertiesContainer> adapter = moshi.adapter(ShowPropertiesContainer.class);
              String encoded =
              "{"mobile_accounts_account_showProperties":"[{\"name\": \"current\"},"
              + "{\"name\": \"available\"}]"}";
              ShowPropertiesContainer showPropertiesContainer = adapter.fromJson(encoded);
              }





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                The easiest way is going to be to read in the value as a string and decode it from there. You can make this transparent to your application code with a JsonAdapter, like the ShowPropertiesContainer.ADAPTER below.



                public final class ShowPropertiesContainer {
                public final List<PropertyConfig> showProperties;

                public static final Object ADAPTER = new Object() {
                @FromJson public ShowPropertiesContainer fromJson(JsonReader reader,
                JsonAdapter<ShowPropertiesContainer> fooAdapter,
                JsonAdapter<List<PropertyConfig>> propertyConfigListAdapter) throws IOException {
                ShowPropertiesContainer showPropertiesContainer = fooAdapter.fromJson(reader);
                return new ShowPropertiesContainer(propertyConfigListAdapter.fromJson(
                showPropertiesContainer.mobile_accounts_account_showProperties),
                showPropertiesContainer.mobile_accounts_account_showProperties);
                }
                };

                final String mobile_accounts_account_showProperties;

                ShowPropertiesContainer(List<PropertyConfig> showProperties,
                String mobile_accounts_account_showProperties) {
                this.showProperties = showProperties;
                this.mobile_accounts_account_showProperties = mobile_accounts_account_showProperties;
                }
                }

                public final class PropertyConfig {
                public final String name;

                PropertyConfig(String name) {
                this.name = name;
                }
                }

                @Test public void showPropertiesContainer() throws IOException {
                Moshi moshi = new Moshi.Builder()
                .add(ShowPropertiesContainer.ADAPTER)
                .build();
                JsonAdapter<ShowPropertiesContainer> adapter = moshi.adapter(ShowPropertiesContainer.class);
                String encoded =
                "{"mobile_accounts_account_showProperties":"[{\"name\": \"current\"},"
                + "{\"name\": \"available\"}]"}";
                ShowPropertiesContainer showPropertiesContainer = adapter.fromJson(encoded);
                }





                share|improve this answer












                The easiest way is going to be to read in the value as a string and decode it from there. You can make this transparent to your application code with a JsonAdapter, like the ShowPropertiesContainer.ADAPTER below.



                public final class ShowPropertiesContainer {
                public final List<PropertyConfig> showProperties;

                public static final Object ADAPTER = new Object() {
                @FromJson public ShowPropertiesContainer fromJson(JsonReader reader,
                JsonAdapter<ShowPropertiesContainer> fooAdapter,
                JsonAdapter<List<PropertyConfig>> propertyConfigListAdapter) throws IOException {
                ShowPropertiesContainer showPropertiesContainer = fooAdapter.fromJson(reader);
                return new ShowPropertiesContainer(propertyConfigListAdapter.fromJson(
                showPropertiesContainer.mobile_accounts_account_showProperties),
                showPropertiesContainer.mobile_accounts_account_showProperties);
                }
                };

                final String mobile_accounts_account_showProperties;

                ShowPropertiesContainer(List<PropertyConfig> showProperties,
                String mobile_accounts_account_showProperties) {
                this.showProperties = showProperties;
                this.mobile_accounts_account_showProperties = mobile_accounts_account_showProperties;
                }
                }

                public final class PropertyConfig {
                public final String name;

                PropertyConfig(String name) {
                this.name = name;
                }
                }

                @Test public void showPropertiesContainer() throws IOException {
                Moshi moshi = new Moshi.Builder()
                .add(ShowPropertiesContainer.ADAPTER)
                .build();
                JsonAdapter<ShowPropertiesContainer> adapter = moshi.adapter(ShowPropertiesContainer.class);
                String encoded =
                "{"mobile_accounts_account_showProperties":"[{\"name\": \"current\"},"
                + "{\"name\": \"available\"}]"}";
                ShowPropertiesContainer showPropertiesContainer = adapter.fromJson(encoded);
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 27 at 0:56









                Eric Cochran

                4,06153161




                4,06153161






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414280%2fcant-parse-json-string-with-moshi%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Berounka

                    Sphinx de Gizeh

                    Different font size/position of beamer's navigation symbols template's content depending on regular/plain...