【问题标题】:How to Load an Array from .json file into App如何将 .json 文件中的数组加载到 App 中
【发布时间】:2020-07-01 20:44:12
【问题描述】:

我的计划是通过 tensorflow 分析图像并将结果发送到应用程序。 Der 结果写入 json 文件。 json文件长这样:

{"file": "image.jpg", "objects": [{"bbox": [177, 14, 508, 773], "label": "spanishguitar", "prob": 0.7284}]

我在 android studio 中的课程是:

`

    private class Downloadjson extends AsyncTask<Void, Void, Void> {
    String name;
    String label;

  public Downloadjson(String name) {
        this.name = name;

    }

    @Override
    protected Void doInBackground(Void... params) {
        String url = SERVER_ADRESS + name;
        sleep(5000);
        //animation start

        for (int i = 0; i < 600; i++) {
            HttpClient client = new DefaultHttpClient(getHttpRequestParams());
            HttpGet getJson = new HttpGet(url);
            try {
                HttpResponse jsonResponse = client.execute(getJson);
                if (200 == jsonResponse.getStatusLine().getStatusCode()) {
                    try {

                        InputStream inputStream = jsonResponse.getEntity().getContent();
                        String json = IOUtils.toString(inputStream);
                        Downloadjson downloadjson = new Gson().fromJson(json, Downloadjson.class);
                        String label = downloadjson.label;


                        TextView Result = (TextView) findViewById(R.id.textView);
                        Result.setText("Your instrument could be a " + downloadjson.label);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }





                }
            } catch (Exception e) {
                e.printStackTrace();

            }



            //exucte
            sleep(5000);

        }
        //stop animation


        return null;
    }

`

当我将“标签”更改为“文件”时,它可以工作并且输出是“image.jpg”。但我需要输出“spanishguitar”的“标签”数组。所以,我认为问题在于,对象不是“标签”而是“对象”(在我的 json 文件中)。 label 是一个数组?你知道我必须改变什么才能获得“标签”吗?

【问题讨论】:

  • “加载到应用程序”如何?您已将此标记为 javascript,但这些都不是 javascript 逻辑

标签: java json android-studio


【解决方案1】:

据我了解,您只需要正确遍历 json 并为 label 赋值。请尝试:

String label = downloadjson.objects[0].label;

【讨论】:

  • 谢谢!但更改为InputStream inputStream = jsonResponse.getEntity().getContent(); String json = IOUtils.toString(inputStream); Downloadjson downloadjson = new Gson().fromJson(json, Downloadjson.class); String label = downloadjson.objects[0].label; 后,错误为:“array type expected found java.lang.string”
猜你喜欢
  • 2013-05-30
  • 1970-01-01
  • 2019-12-29
  • 1970-01-01
  • 1970-01-01
  • 2013-02-15
  • 2015-07-04
  • 1970-01-01
  • 2022-01-25
相关资源
最近更新 更多