【问题标题】:Why can't I parse the JSON?为什么我无法解析 JSON?
【发布时间】:2018-11-27 11:39:04
【问题描述】:

对不起,我是初学者。

我尝试将值放入字符串 name[], int age [], int hand[], 但是所有的值都是空的。

我不知道哪个部分有问题。因为我是从 youtube 上学到的。

我只想将数据存储到数组中。

谢谢

public class main extends AppCompatActivity {
String oName[] = new String []; 
int oAge [] = new int [];
int oHand [] = new int [];

protected void onCreate(Bundle savedInstanceState) {...}

private class DownloadTask extends AsyncTask<String, Integer, String> {
    @Override
    protected String doInBackground (String... values) { 
        InputStream is = null;
        String result = ""; //Get json?
        String line;
        try {
            URL ua = new URL("https://api.myjson.com/bins/r5kim"); //json address
            HttpURLConnection conn = (HttpURLConnection) ua.openConnection();
            conn.setRequestMethod("GET");
            conn.connect();
            is = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            while ((line = reader.readLine()) != null) {
                result += line;
            }
            is.close();
           JSONObject jo = new JSONObject(result); //get json
           JSONArray op = jo.getJSONArray("opponents"); //json array?? parse??
            for(int i = 0; i < op.length(); i++) {
                oName[i] = op.getJSONObject(i).getString("name"); //Get null
                oAge[i] = op.getJSONObject(i).getInt("age"); //Get null
                oHand[i] = op.getJSONObject(i).getInt("hand"); //Get null
                }
            } catch (MalformedURLException e) { //exception
                e.printStackTrace();
            } catch (IOException e) { //exception
                e.printStackTrace();
            } catch (JSONException e) { //exception
                e.printStackTrace();
            }
        return null;
    }
}

【问题讨论】:

  • 您遇到的错误是什么?同时发布日志
  • 另外,尝试记录 'result' string ,然后是 'jo' 和 'op' 。看看你得到了什么结果。
  • 在上述操作之后,我尝试从 oName 数组中获取值到 textview,然后应用程序停止。顺便说一句,我会尝试记录字符串。谢谢

标签: android json


【解决方案1】:

由于您没有发布 json 字符串,因此很难确定您做错了什么。

我能给你的最好建议是查看gson library,这是 Android 上最常见的 json 解析器(还有更多 obv,这是我使用的)。 how to add reference.

观看视频、在 google 上搜索以及在 SO 上搜索,您会发现数以千计的“如何使用 gson 帖子”,但无论如何我都会为您提供最快的“操作方法”。

1.假设你有一个这样的字符串:

{
   "id":1,
   "name":"Little Mouse"
}

2。创建一个与json字符串传递的对象匹配的对象

public class MyObject{
    private int id;
    private String name;
    //getters and setters
}

3.现在初始化 gson 并将字符串解析为所需的对象

//Initialize Gson object with GsonBuilder (you can customize it, you will learn)
Gson gson = new GsonBuilder().create();

//parse your string using an object matching your output
MyObject myObject = gson.fromJson(myJsonString, MyObject.class);

这很简单,但是如果您需要帮助,请随意询问

后记:

您可以使用自定义参数和嵌套类来解析任何类型的类。您只需要创建一个模型类,并使用相同名称(或者您可以显式指定所需名称)和相同属性类型(int、string、...)编写所需的参数

这是你的模特:

public class myJsonModel{

    private List<Opponent> opponents;

    //getters and setters
}

public class Opponent{
    private String name;
    private String age;
    private String hand;
    //getters and setters
}

所以你的结果应该是

myJsonModel myObject = gson.fromJson(myJsonString, myJsonModel.class);

【讨论】:

  • 好的,谢谢我会理解gson,顺便说一句我已经发布了json的url
  • @SiuMingLau 在回答中添加了您的具体案例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
  • 2021-08-27
相关资源
最近更新 更多