【问题标题】:Could someone help me with parsing this JSON file?有人可以帮我解析这个 JSON 文件吗?
【发布时间】:2011-12-14 19:27:12
【问题描述】:

我有这个 JSON 文件:

 {
    "ics_desire":{
    "version":"Beta 0.1.1",
    "description":"description here",
    "build-fingerprint":"fingerprint"
 }

现在我想将 version 部分放在 txtVersion 文本视图中,将 description 部分放在 txtDescription 文本视图中。

有人会帮助我吗?

【问题讨论】:

  • 如果您希望有人能够帮助您,您可能应该粘贴 JSON 文件的内容...
  • 首先,确保你的 JSON 是有效的。现在,它不是。它有 2 个左括号和 1 个右括号。另外,到目前为止,您尝试过什么?这种 JSON 数据量并不复杂,因此任何数量的教程都可以帮助您。

标签: android json parsing textview


【解决方案1】:

首先要说明上面所说的 JSON 代码格式不正确。您可以通过转到http://json.parser.online.fr/ 来验证您的 JSON 代码格式是否正确。 您正确的 JSON 如下(您缺少结束 })

{
"ics_desire": {
    "version": "Beta 0.1.1",
    "description": "description here",
    "build-fingerprint": "fingerprint"
}
}

接下来,这是我过去用来测试的有效 JSON 代码示例。

{
"HealthySubstituteData": [
    {
        "Assoc_ID": "1",
        "uFood": "White Flour",
        "hFood": "Wheat Flour",
        "Category": "Baking",
        "Description": "You can substitute blah blah blah...",
        "Count": "5",
        "submittedBy": "Administrator"
    }
]
}

这是我用来获取数据的代码。

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
    JSONObject json = JSONfunctions.getJSONfromURL("http://your.url.com/whaterver");

    try
    {
        JSONArray  healthySubstituteData = json.getJSONArray("HealthySubstituteData");



        for(int i=0;i<healthySubstituteData.length();i++)
        {                       
            HashMap<String, String> map = new HashMap<String, String>();    
            JSONObject e = healthySubstituteData.getJSONObject(i);



            map.put("Assoc_ID", (String) e.get("Assoc_ID"));
            map.put("uFood", (String) e.get("uFood"));
            map.put("hFood", (String) e.get("hFood"));
            map.put("category", (String) e.get("Category"));
            map.put("description", (String) e.get("Description"));
            map.put("count", (String) e.get("Count"));
            map.put("submittedBy", (String) e.get("submittedBy"));
            mylist.add(map);            
        }       
    }

所以现在我得到了一个 HashMap 类型的数组列表,那时我可以用它做任何我想做的事情。

【讨论】:

    【解决方案2】:

    您可以创建一个简单的类,例如

    public class Myclass{
    String version;
    String description;
    String build-fingerprint;
    };
    

    然后使用 Gson 将 json 转换为您的对象,反之亦然:

    Gson gson = new Gson();
    Myclass obj = gson.fromJson(json_string, Myclass.class);
    

    json_string 应该包含你的 jason 字符串。 现在您可以使用 getter 获取您的类对象值。

    注意:Gson是需要导入的外部库,从google下载。

    【讨论】:

      【解决方案3】:

      忽略 JSON 中缺少的括号,此代码将帮助您完成

      JSONObject json = JSONfunctions.getJSONfromURL("http://your.url.com/whaterver"); //the site where you get your JSON
      JSONObject daata = json.getJSONObject("ics_desire"); //grabbing the inner object
      String version = daata.getString("version");         //grabbing values
      String description = daata.getString("description");
      txtVersion.setText(version);                         //Hope you have initialized the Views.
      txtDescription.setText(description);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-08
        • 2018-08-13
        • 2015-05-31
        • 2016-07-09
        • 2023-03-19
        相关资源
        最近更新 更多