-1
   {
    "-KrwJP0nYf1IUqY-YdXo": {
        "desc": "pdf desc 4",
        "downloadUrl": "https://firebasestorage.googleapis.com/v0/b/fireapp-a889c.appspot.com/o/PdfFile%2F1503177372?alt=media&token=36436f90-8a90-42f2-bf9d-35ac121b9ca7",
        "title": "pdf 4",
        "uid": "0HEj0gbzmcgqNc9nSXAIHqjo15p1",
        "username": "userq"
    },
    "-KrwK2DSmF7UA-SD-VOM": {
        "desc": "asdadadad",
        "downloadUrl": "https://firebasestorage.googleapis.com/v0/b/fireapp-a889c.appspot.com/o/PdfFile%2F37?alt=media&token=701d3aca-8e9e-48c8-ae45-5aab4b4c37ad",
        "title": "adsasdadad",
        "uid": "0HEj0gbzmcgqNc9nSXAIHqjo15p1",
        "username": "userq"
    },
    "-KrwL69kWDatEftRf3C0": {
        "desc": "sdfsfd",
        "downloadUrl": "https://firebasestorage.googleapis.com/v0/b/fireapp-a889c.appspot.com/o/PdfFile%2F1503177856?alt=media&token=e0dea2ff-8311-4c00-9c1b-6402cb69e929",
        "title": "sdf",
        "uid": "0HEj0gbzmcgqNc9nSXAIHqjo15p1",
        "username": "userq"
    },
    "-KrwLdA21nomZUToFOjh": {
        "desc": "gdfgdfgdfgdfg",
        "downloadUrl": "https://firebasestorage.googleapis.com/v0/b/fireapp-a889c.appspot.com/o/PdfFile%2F1503177996?alt=media&token=7d15cb11-f7c0-4086-aefe-5c39964ab83f",
        "title": "dfgdfgdfghdf",
        "uid": "0HEj0gbzmcgqNc9nSXAIHqjo15p1",
        "username": "userq"
    }
}

它是我的 json 文本。我如何在 android 中使用改造进行解析。它具有唯一的对象名称。

我的 json 地址:https ://fireapp-a889c.firebaseio.com/PdfContent.json

4

2 回答 2

0

编辑

在 build.gradle 文件中添加依赖项

compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.2.4'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'

由于 firebase JSON 是一张地图Map<String,Object>,您可以通过以下方式检索数据

public final class ApiService {
private  static Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://fireapp-a889c.firebaseio.com/")
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .build();

public static Retrofit getRetrofit() {
    return retrofit;
}
}

请求接口:

public interface FirebaseApi {
    @GET("PdfContent.json")
    Observable<Map<String, Object>> getFirebaseResponse();
}

然后在需要的地方调用它:

 ApiService.getRetrofit().create(FirebaseApi.class)
            .getFirebaseResponse()
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<Map<String, Object>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                }

                @Override
                public void onNext(Map<String, Object> response) {
                    Log.e("TAG", "onNext: " + response);
                }
            });

它准确地返回您需要的内容。之后,您可以使用迭代器将输出日期转换为具有适当 ID 的项目列表。

于 2017-08-22T15:43:32.717 回答
0
String jsonData = "{\n" +
            "    \"-KrwJP0nYf1IUqY-YdXo\": {\n" +
            "        \"desc\": \"pdf desc 4\",\n" +
            "        \"downloadUrl\": \"https://firebasestorage.googleapis.com/v0/b/fireapp-a889c.appspot.com/o/PdfFile%2F1503177372?alt=media&token=36436f90-8a90-42f2-bf9d-35ac121b9ca7\",\n" +
            "        \"title\": \"pdf 4\",\n" +
            "        \"uid\": \"0HEj0gbzmcgqNc9nSXAIHqjo15p1\",\n" +
            "        \"username\": \"userq\"\n" +
            "    },\n" +
            "    \"-KrwK2DSmF7UA-SD-VOM\": {\n" +
            "        \"desc\": \"asdadadad\",\n" +
            "        \"downloadUrl\": \"https://firebasestorage.googleapis.com/v0/b/fireapp-a889c.appspot.com/o/PdfFile%2F37?alt=media&token=701d3aca-8e9e-48c8-ae45-5aab4b4c37ad\",\n" +
            "        \"title\": \"adsasdadad\",\n" +
            "        \"uid\": \"0HEj0gbzmcgqNc9nSXAIHqjo15p1\",\n" +
            "        \"username\": \"userq\"\n" +
            "    },\n" +
            "    \"-KrwL69kWDatEftRf3C0\": {\n" +
            "        \"desc\": \"sdfsfd\",\n" +
            "        \"downloadUrl\": \"https://firebasestorage.googleapis.com/v0/b/fireapp-a889c.appspot.com/o/PdfFile%2F1503177856?alt=media&token=e0dea2ff-8311-4c00-9c1b-6402cb69e929\",\n" +
            "        \"title\": \"sdf\",\n" +
            "        \"uid\": \"0HEj0gbzmcgqNc9nSXAIHqjo15p1\",\n" +
            "        \"username\": \"userq\"\n" +
            "    },\n" +
            "    \"-KrwLdA21nomZUToFOjh\": {\n" +
            "        \"desc\": \"gdfgdfgdfgdfg\",\n" +
            "        \"downloadUrl\": \"https://firebasestorage.googleapis.com/v0/b/fireapp-a889c.appspot.com/o/PdfFile%2F1503177996?alt=media&token=7d15cb11-f7c0-4086-aefe-5c39964ab83f\",\n" +
            "        \"title\": \"dfgdfgdfghdf\",\n" +
            "        \"uid\": \"0HEj0gbzmcgqNc9nSXAIHqjo15p1\",\n" +
            "        \"username\": \"userq\"\n" +
            "    }\n" +
            "}";
    try {
        JSONObject object = new JSONObject(jsonData);
        Iterator<String> iterator = object.keys();
        while (iterator.hasNext()) {
            JSONObject obj = object.getJSONObject(iterator.next());
            Log.d("TAG", obj.toString());
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

在这里,您只需将您的 JSON 字符串传递给jsonData 这样,您就会得到类似的输出,

输出:

{"desc":"pdf desc 4","downloadUrl":"https:\/\/firebasestorage.googleapis.com\/v0\/b\/fireapp-a889c.appspot.com\/o\/PdfFile%2F1503177372?alt=media&token=36436f90-8a90-42f2-bf9d-35ac121b9ca7","title":"pdf 4","uid":"0HEj0gbzmcgqNc9nSXAIHqjo15p1","username":"userq"}
{"desc":"asdadadad","downloadUrl":"https:\/\/firebasestorage.googleapis.com\/v0\/b\/fireapp-a889c.appspot.com\/o\/PdfFile%2F37?alt=media&token=701d3aca-8e9e-48c8-ae45-5aab4b4c37ad","title":"adsasdadad","uid":"0HEj0gbzmcgqNc9nSXAIHqjo15p1","username":"userq"}
{"desc":"sdfsfd","downloadUrl":"https:\/\/firebasestorage.googleapis.com\/v0\/b\/fireapp-a889c.appspot.com\/o\/PdfFile%2F1503177856?alt=media&token=e0dea2ff-8311-4c00-9c1b-6402cb69e929","title":"sdf","uid":"0HEj0gbzmcgqNc9nSXAIHqjo15p1","username":"userq"}
{"desc":"gdfgdfgdfgdfg","downloadUrl":"https:\/\/firebasestorage.googleapis.com\/v0\/b\/fireapp-a889c.appspot.com\/o\/PdfFile%2F1503177996?alt=media&token=7d15cb11-f7c0-4086-aefe-5c39964ab83f","title":"dfgdfgdfghdf","uid":"0HEj0gbzmcgqNc9nSXAIHqjo15p1","username":"userq"}
于 2017-08-22T15:59:28.227 回答