【问题标题】:JSONObject from string on AndroidAndroid上字符串中的JSONObject
【发布时间】:2013-08-14 11:33:50
【问题描述】:

l 从服务器获取带有答案的字符串。我想做JSONObject。

我做

JSONObject jsonObj = new JSONObject(json);

在json中有

"{"sentences":[{"trans":"R\u0455R\u0491ReR\u0405","orig":"�\u0455�\u0491��\u0405","translit":""," src_translit":"R\u1E91Rg\u0300RoR\u1E90"}],"src":"ru","server_time":1}"

但 jsonObj 有

"{"sentences":[{"src_translit":"RẑRg̀RoRẐ",​​"orig":"�ѕ�ґ��Ѕ","trans":"RѕRґReRЅ","translit":""}], "server_time":1,"src":"ru"}"

那么我如何从“trans”中获得价值:“RѕRґReRЅ”?

PS RѕRґReRЅ 在普通字符集中它是“自己的”

【问题讨论】:

    标签: android json getjson


    【解决方案1】:

    如果你已经有了 jsonObj,你只需要这样做:

      try {
    // Getting Array of Sentences
    sentences = json.getJSONArray("sentences");
    
    // looping through All Contacts
    for(int i = 0; i < sentences.length(); i++){
        JSONObject c = sentences.getJSONObject(i);
    
        // get the value from trans
        String trans = c.getString("trans");       
      //now you should save this string in an array 
    
    }
     } catch (JSONException e) {
        e.printStackTrace();
        }
    

    【讨论】:

    • 但我在“RѕRґReRЅ”中有反式,但必须有“自己的”
    • 你从哪里得到你的 json?你的国家字符集是什么?如果您需要转换为,比如说,utf-8,您必须:URLEncoder.encode(json_string, "UTF-8");
    • 我有错误:" 解析数据时出错 org.json.JSONException: 值 %7B%22sentences%22%3A%5B%7B%22trans%22%3A%22R%5Cu0455R%5Cu0491ReR%5Cu0405% 22%2C%22orig%22%3A%22%EF%BF%BD%5Cu0455%EF%BF%BD%5Cu0491%EF%BF%BD%EF%BF%BD%5Cu0405%22%2C%22translit%22% 3A%22%22%2C%22src_translit%22%3A%22R%5Cu1E91Rg%5Cu0300RoR%5Cu1E90%22%7D%5D%2C%22src%22%3A%22ru%22%2C%22server_time%22%3A24%7D% java.lang.String 类型的 0A 无法转换为 JSONObject"
    • 在编码json字符串之前是" {"sentences":[{"trans":"R\u0455R\u0491ReR\u0405","orig":"�\u0455�\u0491��\u0405 ","translit":"","src_translit":"R\u1E91Rg\u0300RoR\u1E90"}],"src":"ru","server_time":24}"
    • 在执行任何操作之前,您必须删除引号 (" ")。其次,您必须将收到的字符集编码为适当的字符集。看看这个:illegalargumentexception.blogspot.pt/2009/05/…
    【解决方案2】:

    试试这个

      JSONObject jsonObj = new JSONObject(json);
      JSONArray  sentences = jsonObj.getJSONArray("sentences");
      for(int i=0;i<sentences.length();i++){
        JSONObject number = sentences.getJSONObject(i);
        String transValue = number.getString("trans");
    }
    

    【讨论】:

      【解决方案3】:

      首先,您必须从 JSON 字符串 (json) 的开头和结尾删除单引号形式,这样您的 JSON 将是有效的 json,您必须这样做:

      json= json.substring(1, json.length()-1);
      

      之后你会这样做:

      JSONObject oJsonObject = new JSONObject(json);
      JSONArray  oJsonArray   = oJsonObject.getJSONArray("sentences");
      
      for(int i=0; i<oJsonArray.length(); i++)
      {
          JSONObject oJsonObject1 = oJsonArray.getJSONObject(i);
          String transValue = oJsonObject1 .getString("trans");
      }
      

      【讨论】:

      • 关于 JSONObject oJsonObject = new JSONObject(json);我有错误:解析数据时出错 org.json.JSONException:java.lang.String 类型的值语句无法转换为 JSONObject
      • "{"sentences":[{"trans":"R\u0455R\u0491ReR\u0405","orig":"�\u0455�\u0491��\u0405","translit" :"","src_translit":"R\u1E91Rg\u0300RoR\u1E90"}],"src":"ru","server_time":1}"
      • 再次,你的 json 最后在字符串的开头显示双引号,正如我提到的,你必须在解析之前删除那些你只能解析这个:{“sentences”:[{“trans” :"R\u0455R\u0491ReR\u0405","orig":"�\u0455�\u0491��\u0405‌​","translit":"","src_translit":"R\u1E91Rg\u0300RoR\u1E90"} ],"src":"ru","server_ti‌​me":1}
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-03
      • 2011-03-25
      相关资源
      最近更新 更多