Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 通过Moshi转换为Json的嵌套数组解析_Arrays_Json_Parsing_Moshi - Fatal编程技术网

Arrays 通过Moshi转换为Json的嵌套数组解析

Arrays 通过Moshi转换为Json的嵌套数组解析,arrays,json,parsing,moshi,Arrays,Json,Parsing,Moshi,我有一个这种格式的JSON字符串 { "user": "sam", "password": "abcd1234", "categories": [ { "fruit name": "watermelon" }, { "fruit name":"jackfruit" }, { "fruit name": "kiwi" } ], "store":"S

我有一个这种格式的JSON字符串

{
  "user": "sam",
  "password": "abcd1234",
  "categories":
    [
      {
        "fruit name": "watermelon"
      },
      {
        "fruit name":"jackfruit"
      },
      {
        "fruit name": "kiwi"
      }
    ],
  "store":"SPROUTS"
}
我在想我创建了一个这样的结构类

class Structure {
  String user;
  String password;
  String store;

  private Structure() {
    this.user = "sam";
    this.password = "abcd1234";
    this.store = "SPROUTS";
  }
}
要解析JSON,我只需通过Moshi通过以下代码行即可:

Moshi moshi = new Moshi.Builder().build();
Structure structure = new Structure();
String json = moshi.adapter(Structure.class).indent(" ").toJson(structure);

但是,我还想将给定JSON中的类别传递到这个函数中。如何使用具有相同代码的类别?另外,需要对我的类结构进行哪些修改?

使用Java类来表示类别JSON对象,就像您对结构JSON对象所做的那样

public final class Structure {
  public final String user;
  public final String password;
  public final String store;
  public final List<Category> categories;

  Structure(String user, String password, String store, List<Category> categories) {
    this.user = user;
    this.password = password;
    this.store = store;
    this.categories = categories;
  }

  public static final class Category {
    @Json(name = "fruit name") public final String fruitName;

    Category(String fruitName) {
      this.fruitName = fruitName;
    }
  }

  public static void main(String[] args) throws Exception {
    Moshi moshi = new Moshi.Builder().build();
    JsonAdapter<Structure> adapter = moshi.adapter(Structure.class);
    String json = "{\n"
        + "  \"user\": \"sam\",\n"
        + "  \"password\": \"abcd1234\",\n"
        + "  \"categories\":\n"
        + "    [\n"
        + "      {\n"
        + "        \"fruit name\": \"watermelon\"\n"
        + "      },\n"
        + "      {\n"
        + "        \"fruit name\":\"jackfruit\"\n"
        + "      },\n"
        + "      {\n"
        + "        \"fruit name\": \"kiwi\"\n"
        + "      }\n"
        + "    ],\n"
        + "  \"store\":\"SPROUTS\"\n"
        + "}";
    Structure value = adapter.fromJson(json);
  }
}
公共最终类结构{
公共最终字符串用户;
公共最终字符串密码;
公共最终字符串存储;
公开最终名单类别;
结构(字符串用户、字符串密码、字符串存储、列表类别){
this.user=用户;
this.password=密码;
this.store=商店;
这个。类别=类别;
}
公共静态最终类类别{
@Json(name=“fruit name”)公共最终字符串fruit name;
类别(字符串名称){
this.fruthName=fruthName;
}
}
公共静态void main(字符串[]args)引发异常{
Moshi-Moshi=新的Moshi.Builder().build();
JsonAdapter adapter=moshi.adapter(Structure.class);
字符串json=“{\n”
+“\'user\':\'sam\',\n”
+“密码”:“abcd1234\,\n”
+“\”类别\“:\n”
+“[\n”
+“{\n”
+“\”水果名\“:\”西瓜\“\n”
+},\n
+“{\n”
+“\'水果名称\':\'菠萝蜜\”\n”
+},\n
+“{\n”
+“\”水果名\“:\”猕猴桃\“\n”
+“}\n”
+“],\n”
+“\'store\':\'SPROUTS\”\n”
+ "}";
结构值=adapter.fromJson(json);
}
}

非常感谢@Eric Cochran。实际上我自己也做了类似的事情。忘记更新这里的评论:).class Structure{String user;String password;String store;List Categories;private Structure(){this.user=“sam”;this.password=“abcd1234”;this.store=“SPROUTS”;this.Categories=新类别(“香蕉”)}私有类类别{String fruitName;私有类别(String fruit){fruit name=fruit;}