2012-11-07 31 views
2

我是新来的Java中的JSON操作,我有一个JSON数组形式的字符串,有几个层需要访问并放入类属性中。例如,这里是我的JSON对象:JSON数组字符串到Java中的JSONArray对象

{"JsonObject" : [{"attributeOne":"valueOne", 
        "attributeTwo":"valueTwo", 
        "attributeThree":[{"subAttributeOne":"subValueOne", 
            "subAttributeTwo":"subValueTwo"}], 
        "attributeFour":[{"subAttributeOne":"subValueThree", 
            "subAttributeTwo":"subValueFour"}], 
        "attributeFive":"valueThree"}, 
       {"attributeOne":"valueFour", 
        "attributeTwo":"valueFive", 
        "attributeThree":[{"subAttributeOne":"subValueFive", 
            "subAttributeTwo":"subValueSix"}], 
        "attributeFour":[{"subAttributeOne":"subValueSeven", 
            "subAttributeTwo":"subValueEight"}], 
        "attributeFive":"valueSix"}]} 

可以说我有一类称为MyClass的具有这些属性,我如何解析这个字符串,知道这是N个对象的数组,每个包含“attributeOne,attributeTwo ,...,attributeFive“?

这是我到目前为止有:

public MyClass[] jsonToJava (String jsonObj) 
{ 
    ArrayList<MyClass> myClassArray = new ArrayList<MyClass>(); 


    //Somehow create a JSONArray from my jsonObj String 
    JSONArray jsonArr = new JSONArray(jsonObj); //Don't know if this would be correct 

    for(int i=0; i<jsonArr.length; i++){ 
     MyClass myClassObject = new MyClass(); 
     myClassObject.setAttributeOne = jsonArr[i].getString("attributeOne"); 
     // How can I access the subAttributeOne and Two under attributeThree and Four? 
     // add all other values to myClassObject 
     myClassArray.add(myClassObject); 
    } 
    return myClassArray; 
} 

正如你可能会说,我是相当新的编程:P在此先感谢您的帮助!

+0

为什么不使用'GSON'?其简单快捷。你只需要将所有的类转换为String,反之亦然。 –

+0

杰克逊比gson更快,而且使用起来也很简单。 – digitaljoel

回答

1

为了您的例子,你可以使用递归类似:

public Object getChild(Object parent, int index) { 

    if (parent instanceof JSONArray) {   

     try { 
      Object o = ((JSONArray)parent).get(index); 

      if(o instanceof JSONObject){ 
       parent = ((JSONObject) (o)).getMap();     
       return parent; 
      } 

      if(o instanceof Double){ 
       parent = (Double) o;     
       return parent; 
      } 

      if(o instanceof Integer){ 
       parent = (Integer) o;     
       return parent; 
      } 
          .... 


     } catch (JSONException e1) { 
      e1.printStackTrace(); 
     }  
    } 



    if (parent instanceof JSONObject) {    
     parent = ((JSONObject)parent).getMap(); 
    } 

    if (parent instanceof Map<?, ?>) { 
     Map<?, ?> map = (Map<?, ?>) parent; 
     Iterator<?> it = map.keySet().iterator(); 
     for (int i=0; i<index; i++){ 
      it.next(); 
     } 

     return map.get(it.next()); 
    } 
    else if (parent instanceof Collection<?>) { 
     Iterator<?> it = ((Collection<?>) parent).iterator(); 

     for (int i=0; i<index; i++){ 
      it.next();    
     } 
     return it.next(); 
    } 
    //throw new IndexOutOfBoundsException("'" + parent + "'cannot have children!"); 
    return null; 
} 

但它有点复杂(+不好的做法,使用instanceof),你不要不想重新发明轮子。所以使用GSON或杰克逊。

Gson gson = new Gson(); 
String myClassStr = gson.toGson(MyClassInstance); 
.... 
    Myclass yourClass = gson.fromJson(myClassStr, Myclass.class); 
2

尝试杰克逊JSON:

ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally 
User user = mapper.readValue(jsonObj, User.class); //method overloaded to take String 

从抓住这个两个班轮:

http://wiki.fasterxml.com/JacksonInFiveMinutes

http://jackson.codehaus.org/0.9.9/javadoc/org/codehaus/jackson/map/ObjectMapper.html

如果你的JSON强转换为对象。在Java EE上下文中,您可以使用适当的注释在端点获得解组功能。

+0

费斯打败我吧! – Alex