【问题标题】:Convert string to json object using java使用java将字符串转换为json对象
【发布时间】:2014-09-09 11:01:31
【问题描述】:

我有一个字符串数据,我想将它添加到 json 并检索它以将其添加到 excel。但它没有发生 请帮助 这是我的代码....

字符串表 = "[{"id":1,"name":"xxx","location":"xx"}{"id":2,"name":"yyy","location":"yy"}{" id":3,"name":"zzz","location":"zz"}]";

 JSONObject jObject  = new JSONObject(table); // json       String
 projectname = jObject.getString("name"); // get the name from data.
 System.out.println(projectname);

【问题讨论】:

  • 这不是一个有效的字符串。
  • 此问题已在此链接上得到解答。 stackoverflow.com/questions/5245840/… 这也不是丹尼斯建议的有效字符串。
  • 将 POJO 与 ObjectMapper 一起使用。这非常简单易懂..
  • 对不起,我在添加问题时犯了一个错误。但在我的代码中它是正确的

标签: java json


【解决方案1】:

您可以使用 JSONObject 和 JsonArray ,如下所示

JSONObject mainObj = new JSONObject();
    JSONArray somearr = new JSONArray();
                        while (iter.hasnext()) {
                             ClassName someObject=iter.next();
                            JSONObject jsonobj = new JSONObject();
                            jsonobj.put("id", id_from_someObject);;
                            jsonobj.put("name", name_from_someObject);
                            jsonobj.put("name", location_from_someObject);;

                        somearr.put(seatObj);

                        }

mainObj.put("response",somearr);

return mainObj.toString();

【讨论】:

    【解决方案2】:

    简单有效的方法我会告诉你使用json.orgjackson-mapper-asl首先创建一个类假设它的名字是Person

    public class Person {
        private int id;
        private String name;
        private String location;
        //getters and setters
        @Override
        public String toString() {
            return "Person{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", location='" + location + '\'' +
                    '}';
        }
    }
    

    假设主类创建另一个类

    public static void main(String[] args) throws IOException, JSONException {
        String jsonString ="[{\"id\":1,\"name\":\"xxx\",\"location\":\"xx\"},
                                     {\"id\":2,\"name\":\"yyy\",\"location\":\"yy\"},
                                     {\"id\":3,\"name\":\"zzz\",\"location\":\"zz\"}]";
        ObjectMapper mapper = new ObjectMapper();
        JSONArray jsonArray = new JSONArray(jsonString);
        List<Person> listFromJsonArray = new ArrayList<Person>();
        for(int i =0 ;i<jsonArray.length();i++){
            String firstObjectAsString = jsonArray.get(i).toString();
            Person person = mapper.readValue(mapper.readTree(firstObjectAsString), 
                                                                       Person.class);
            listFromJsonArray.add(person);
        }
        System.out.println(listFromJsonArray);
    
    }
    

    现在使用列表获取单个 Person 对象,使用 getter 获取单个值并做任何你想做的事情

    【讨论】:

      【解决方案3】:

      嗯,你在定义你的表字符串时确实有一个错误。我猜你需要

      String table = "[{'id':1,'name':'xxx','location':'xx'}{'id':2,'name':'yyy','location':'yy'}{'id':3,'name':'zzz','location':'zz'}]";
      JSONObject jObject = new JSONObject(table);
      

      而不是

      String table = [{"id":1,"name":"xxx","location":"xx"}{"id":2,"name":"yyy","location":"yy"}{"id":3,"name":"zzz","location":"zz"}];
      JSONObject jObject = new JSONObject(table); 
      

      【讨论】:

      • 我做了同样的事情,但它给出了一个错误,说 org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
      • 是的,我将字符串表添加到 JSONArray,然后检索并添加到 JSONObject
      【解决方案4】:

      您可以尝试以下代码来解析 json 对象

      String table = "{"keyElement" : {"id":1,"name":"xxx","location":"xx"}{"id":2,"name":"yyy","location":"yy"}{"id":3,"name":"zzz","location":"zz"}}";
      JSONObject jObject  = new JSONObject(table);
      JSONArray array = jObject.getJSONArray("keyElement");    
      for(int i = 0 ; i < array.length() ; i++)
      {
          System.out.println(array.getJSONObject(i).getString("id"));
          System.out.println(array.getJSONObject(i).getString("name"));
          System.out.println(array.getJSONObject(i).getString("location"));
      }
      

      现在你可以明白了...

      【讨论】:

        猜你喜欢
        • 2011-08-30
        • 1970-01-01
        • 2013-05-19
        • 1970-01-01
        • 1970-01-01
        • 2017-01-24
        • 2017-03-24
        • 2014-08-21
        • 2021-09-18
        相关资源
        最近更新 更多