当前位置: 首页 > 知识库问答 >
问题:

不能用Moshi解析Json字符串

谢奇略
2023-03-14

我试图用Moshi解析一个Json响应,我遇到的问题是键的值是字符串上的Json换行:

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

这是我的课

@Json(name = "mobile_accounts_account_showProperties")
private List<PropertyConfig> showProperties;
public class PropertyConfigJsonAdapter {

public PropertyConfigJsonAdapter() {
}

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

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

    return new ArrayList<>();
}
MoshiConverterFactory.create(new Moshi.Builder().add(new PropertyConfigJsonAdapter()).build())

共有1个答案

阎冠玉
2023-03-14

最简单的方法是以字符串的形式读取值,然后从那里对其进行解码。您可以使用JsonAdapter使其对应用程序代码透明,如下面的ShowPropertiesContainer.adapter

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);
}
 类似资料:
  • 问题内容: 我有一个带有unicode字符的json文件,但我无法解析它。我已经在Flash CS5(JSON库)中进行了尝试,并且在http://json.parser.online.fr/中进行了尝试,但我始终会收到“意外令牌- 评估失败” 抱歉,语法确实存在问题,它是通过客户端发送的。 有人可以帮帮我吗?谢谢 问题答案: RFC: JSON文本应以Unicode编码。默认编码为UTF-8。

  • 问题内容: 我有以下Json字符串 我正在尝试解析它并打印出每个名称和值-最简单的方法是什么?我尝试了jQuery.parseJSON但我不知道如何使用它 示例代码会很棒 问题答案: 结果是: jsFiddle示例:http://jsfiddle.net/bradchristie/XtzjZ/1/

  • 问题内容: 我正在尝试解析用PHP编码并通过TCP发送到C ++客户端的JSON字符串。 我的JSON字符串如下所示: 在C ++客户端上,我正在使用jsoncpp库: 问题是我没有得到任何输出,甚至没有关于解析的错误(如果有)。你能帮我了解我做错了什么吗? 问题答案: 您的问题是:没有 root [“ name”] 。您的文档应如下所示: 和你的代码是这样的: 如果您想按原样保留数据: 使用迭代

  • 问题内容: 我正在尝试解析java中的JSON字符串,以单独打印各个值。但是,在使程序运行时,出现以下错误- 我的班级看起来像- 让我知道我丢失了什么,或者为什么每次运行该应用程序时都会得到该错误。任何意见,将不胜感激。 问题答案: 看我的评论。当以 android.jar身份运行时,您需要包含完整的org.json库。仅包含要针对其进行编译的存根。 __ 此外,您还必须在JSON数据中删除额外的

  • 问题内容: 我正在尝试解析一个JSON结构,如: 也就是说,JSON中的元素是带有转义json的字符串。 所以,我有一些类似的东西 但这崩溃了 这是因为.c的输出是字符串,而不是JSON。如何让jq解析此字符串? 我最初的解决方案是使用sed将替换所有的逃生字符(,和),但凌乱的,我认为有内置的方式做到这一点? 谢谢! 编辑:另外,这里可用的jq版本是: 我想我可以根据需要更新它。 问题答案: j

  • 问题内容: 我在C#中有一个类似以下的字符串。我需要遍历并创建HTML表输出。我尝试使用JSON.NET,但无法弄清楚如何检索键(名称,年龄和工作)。 表格格式为 任何帮助将不胜感激。 Dave提供的代码在这里是理想的解决方案..但是它适用于.NET 4.0 ..我已经将JSON.NET和以下代码用于.NET 3.5 使用Newtonsoft.Json.Linq; 问题答案: 您可以使用.NET