如何遍历jsonString的每个节点

时间:2014-08-25 10:45:53

标签: java json

你能解释一下我在哪里出错,能够遍历每个节点但是获得例外。 以递归方式编写代码遍历jsonstring,

public static void main(String [] args)  {

    String jsonString = "{ \"developers\": [{ \"firstName\":\"Linus\" , \"lastName\":\"Torvalds\" }, " +
                "{ \"firstName\":\"John\" , \"lastName\":\"von Neumann\" } ]}";
        parse(jsonString);

    }

public static void parse(Object jsonString)  {

        try {
            JSONObject jsonObject = new JSONObject(jsonString.toString());
            Iterator<String> iter = jsonObject.keys();
            while (iter.hasNext()) {
                String key = iter.next();
                Object value = jsonObject.get(key);
                System.out.println(key+"==>"+value);
                parse(value);
            }
        } catch (JSONException e) {
            try {
                JSONArray jsonArray = new JSONArray(jsonString.toString());

                for (int i = 0; i < jsonArray.length(); i++)
                {
                    Object value = jsonArray.get(i);
                    System.out.println("**"+value);
                    parse(value);
                }
            } catch (JSONException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }

1 个答案:

答案 0 :(得分:0)

JSON字符串是键值对,许多库提供对遍历的遍历...

以下是其中一个例子。

import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;

class JsonDecodeDemo 
{
   public static void main(String[] args) 
   {
      JSONParser parser=new JSONParser();
      String s = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
      try{
         Object obj = parser.parse(s);
         JSONArray array = (JSONArray)obj;
         System.out.println("The 2nd element of array");
         System.out.println(array.get(1));
         System.out.println();

         JSONObject obj2 = (JSONObject)array.get(1);
         System.out.println("Field \"1\"");
         System.out.println(obj2.get("1"));    

         s = "{}";
         obj = parser.parse(s);
         System.out.println(obj);

         s= "[5,]";
         obj = parser.parse(s);
         System.out.println(obj);

         s= "[5,,2]";
         obj = parser.parse(s);
         System.out.println(obj);
      }catch(ParseException pe){
         System.out.println("position: " + pe.getPosition());
         System.out.println(pe);
      }
   }
}

发现它非常有趣here

同样的例子,我已经格式化了你发布的JSON字符串,

public static void main(String[] args) {
    String jsonString = "{\"developers\":[{\"firstName\":\"Linus\",\"lastName\":\"Torvalds\"},{\"firstName\":\"John\",\"lastName\":\"von Neumann\"}]}";
    parse(jsonString);
}

public static void parse(Object jsonString) {
    try {
        JSONObject jsonObject = new JSONObject(jsonString.toString());
        Iterator<String> iter = jsonObject.keys();
        while (iter.hasNext()) {
            String key = iter.next();
            Object value = jsonObject.get(key);
            System.out.println(key + "==>" + value);
            parse(value);
        }
    }
    catch (JSONException e) {
        try {
            JSONArray jsonArray = new JSONArray(jsonString.toString());
            for (int i = 0; i < jsonArray.length(); i++)
            {
                Object value = jsonArray.get(i);
                System.out.println("**" + value);
                parse(value);
            }
        }
        catch (JSONException e1) {
            // e1.printStackTrace();
        }
        // e.printStackTrace();
    }
}

输出:

developers==>[{"lastName":"Torvalds","firstName":"Linus"},{"lastName":"von Neumann","firstName":"John"}]
**{"lastName":"Torvalds","firstName":"Linus"}
lastName==>Torvalds
firstName==>Linus
**{"lastName":"von Neumann","firstName":"John"}
lastName==>von Neumann
firstName==>John