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())
jsonparser moshi
add a comment |
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())
jsonparser moshi
add a comment |
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())
jsonparser moshi
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
jsonparser moshi
asked Nov 21 at 14:28
Ezequiel Di Sisto
111
111
add a comment |
add a comment |
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);
}
add a comment |
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);
}
add a comment |
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);
}
add a comment |
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);
}
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);
}
answered Nov 27 at 0:56
Eric Cochran
4,06153161
4,06153161
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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