且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Java中将JSON数据转换为字符串

更新时间:2023-01-16 10:12:08

在您的代码中,您从URL读取了json数据。我只是复制了您的数据并将其粘贴到文件中,并在URL断开时读取了该文件。在这里,我一步一步地显示了如何解析json对象和其中的内容。为此,我使用了 java-json-schema.jar

In your code you read the json data from a URL. I just copied your data and pasted it in a file and read the file as your url was down. Here step by step I have shown how to parse your json object and content inside it. For this I used java-json-schema.jar.

    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Iterator;
    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    import org.json.simple.parser.ParseException;
    public class Tets {

        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            JSONParser parser = new JSONParser();
           try{
          /*  URL url = new URL("http://192.168.1.13/test/ProductWb.php?productId=9");
            HttpURLConnection conn ;
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setReadTimeout(60);
            conn.setRequestProperty("Accept", "application/json");*/
            String json="";

            Object obj = parser.parse(new FileReader("C:\\XXX\\XX\\src\\javapackage\\t.json"));

            JSONObject jsonObject = (JSONObject) obj;
            System.out.println(jsonObject.toJSONString());  //modeles object
            JSONArray name = (JSONArray) jsonObject.get("modeles");
            System.out.println(name.toJSONString());//array inside modeles array


            for (Object o : name)
              {
                JSONObject person = (JSONObject) o;
                 JSONObject person1 = (JSONObject)person.get("modele");
                              System.out.println(person.get("modele"));//modele object
                              System.out.println(person1.get("id_lang"));//modele attribute
              } 



        }catch(Exception e){e.printStackTrace();}

        }
    }

OutPut

您的Json对象

{"modeles":[{"modele":{"id_lang":"4","info_5":null,"info_4":null,"link_rewrite":"208","meta_keywords":"peugeot 208","info_3":null,"info_2":null,"info_1":null,"available_now":"","meta_description":"Peugeot 208","id_product":"9","description_short":"<pre>Peugeot 208<\/pre>","description":null,"name":"208","info_prix":"","meta_title":"Peugeot 208","available_later":"","id_shop":"1"}}]}

您的Json数组包含在json对象中

Your Json Array contained in json Object

[{"modele":{"id_lang":"4","info_5":null,"info_4":null,"link_rewrite":"208","meta_keywords":"peugeot 208","info_3":null,"info_2":null,"info_1":null,"available_now":"","meta_description":"Peugeot 208","id_product":"9","description_short":"<pre>Peugeot 208<\/pre>","description":null,"name":"208","info_prix":"","meta_title":"Peugeot 208","available_later":"","id_shop":"1"}}]

数组中的Json对象

{"id_lang":"4","info_5":null,"info_4":null,"link_rewrite":"208","meta_keywords":"peugeot 208","info_3":null,"info_2":null,"info_1":null,"available_now":"","meta_description":"Peugeot 208","id_product":"9","description_short":"<pre>Peugeot 208<\/pre>","description":null,"name":"208","info_prix":"","meta_title":"Peugeot 208","available_later":"","id_shop":"1"}

您的Json对象id_lang属性值= 4

Your Json Object id_lang atrribute value = 4

4