Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中将json转换为xml_Java_Xml_Json - Fatal编程技术网

在java中将json转换为xml

在java中将json转换为xml,java,xml,json,Java,Xml,Json,我试图将json传输到xml文件,但出现了一些问题 代码示例: String strinput= query; ArrayList<String> urls = new ArrayList<>(); String keyofaccount = "mykey"; String bingApiUrlpattern = "https://api.datamarket.azure.com/Bing/Search/Web?Query=%%27%s%%27

我试图将json传输到xml文件,但出现了一些问题

代码示例:

  String strinput= query;
    ArrayList<String> urls = new ArrayList<>();
    String keyofaccount = "mykey";
    String bingApiUrlpattern = "https://api.datamarket.azure.com/Bing/Search/Web?Query=%%27%s%%27&$format=JSON";

    String query = URLEncoder.encode(strinput, Charset.defaultCharset().name());
    String bingUrl = String.format(bingApiUrlpattern, query);

    byte[] accountkeyinbytes = Base64.encodeBase64((keyofaccount + ":" + keyofaccount).getBytes());
    String accountkeyencryp = new String(accountkeyinbytes);
    URL url = new URL(bingUrl);
    URLConnection connection = url.openConnection();
    connection.setRequestProperty("Authorization", "Basic " + accountkeyencryp);

    try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())))
    {
        String inputLine;

        StringBuilder stringbuilder = new StringBuilder();
        while ((inputLine = in.readLine()) != null)
        {
            stringbuilder.append(inputLine);
        }

        JSONObject job= new JSONObject(stringbuilder.toString());
        String xmstr = org.json.XML.toString(job);
        System.out.println(xmlstr);
    }
    catch (JSONException e)
    {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
字符串strinput=query;
ArrayList URL=新的ArrayList();
字符串keyofcount=“mykey”;
字符串bingApiUrlpattern=”https://api.datamarket.azure.com/Bing/Search/Web?Query=%%27%s%%27&$format=JSON”;
String query=URLEncoder.encode(strinput,Charset.defaultCharset().name());
String bingUrl=String.format(bingApiUrlpattern,query);
字节[]accountkeyinbytes=Base64.encodeBase64((keyOfcount+“:“+keyOfcount).getBytes());
String accountkeyencryp=新字符串(accountkeyinbytes);
URL=新URL(bingUrl);
URLConnection=url.openConnection();
connection.setRequestProperty(“授权”、“基本”+accountkeyencryp);
try(BufferedReader in=new BufferedReader(new InputStreamReader(connection.getInputStream()))
{
字符串输入线;
StringBuilder StringBuilder=新的StringBuilder();
而((inputLine=in.readLine())!=null)
{
stringbuilder.append(inputLine);
}
JSONObject作业=新的JSONObject(stringbuilder.toString());
字符串xmstr=org.json.XML.toString(作业);
System.out.println(xmlstr);
}
捕获(JSONException e)
{
e、 printStackTrace();//要更改catch语句的主体,请使用文件|设置|文件模板。
}
我得到的结果是:

<d><results><Description>Give your home, office and life good feng shui with expert advice from Rodika Tchi. Learn about feng shui elements, Chinese zodiac, birthstones &amp; more.</Description><Url>http://fengshui.about.com/</Url><DisplayUrl>fengshui.about.com</DisplayUrl><ID>48333f88-d9e7-4b01-913c-4b8e65e28878</ID><__metadata><type>WebResult</type><uri>https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query=&apos; feng shui&apos;&amp;$skip=0&amp;$top=1</uri></__metadata><Title>Feng Shui - Expert Tips and Advice - About.com</Title></results><results><Description>Feng shui (i pinyin: fēng shuǐ) is a Chinese philosophical system of harmonizing everyone with the surrounding environment. The term feng shui literally translates ...</Description><Url>http://en.wikipedia.org/wiki/Feng_shui</Url>....
在Rodika Tchi的专家建议下,为您的家庭、办公室和生活提供良好的风水。了解风水元素、中国生肖、诞生石和;更多http://fengshui.about.com/fengshui.about.com48333f88-d9e7-4b01-913c-4b8e65e28878WebResulthttps://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query=' 风水学&$跳过=0&$top=1风水-专家提示和建议-关于。风水(拼音:fēng shuǐ)是一种中国哲学体系,旨在协调每个人与周围环境的关系。“风水”一词的字面意思是。。。http://en.wikipedia.org/wiki/Feng_shui....
有人知道我的问题在哪里吗?
我如何才能只得到描述?

您正在打印整个XML字符串-您如何期望它只给出描述?要做到这一点,您需要解析JSON数据或XML

JSONArray results = job.getJSONObject("d").getJSONArray("results");
for (int i = 0; i < results.length(); i++) {
    System.out.println(results.getJSONObject(i).get("Description"));
}
JSONArray results=job.getJSONObject(“d”).getJSONArray(“results”);
对于(int i=0;i
您能描述一下结果的错误吗?它看起来像是有效的XML(快速浏览一下),如果您不以原始JSON格式向我们显示数据,我们怎么知道出了什么问题?在我看来(但我没有花很长时间分析),它似乎是可行的,您的输出看起来像XML,您所说的“出错”是什么意思?例如,如果我搜索查询“卢浮宫在哪里?”。我不知道地点的详细情况,也不认识它。