不能将字符串转换为JsonArray [英] Can't Convert string to JsonArray

查看:197
本文介绍了不能将字符串转换为JsonArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何转换将此String gson.JsonArray?

 一个String =[[110917,3.0099999999999998,-0.72999999999999998,2.8500000000000001,2.96,685.0,38603.0],[110917,2.71,0.20999999999999999,2.8199999999999998,2.8999999999999999,2987.0 ,33762.0]];

这是我的code:

<为pre> com.google.gson *。
 公共静态无效的主要(字串[] args)
   {
       //声明在这儿
       的System.out.println(字符串以JSON数组语句);
       JsonParser分析器=新JsonParser();
       JsonElement tradeElement = parser.parse(s.toString());
       JsonArray贸易= tradeElement.getAsJsonArray();
       的System.out.println(贸易);
    }

这是该集合字符串转换JSonArray的方式?


解决方案

要拥有你的JSON数组中的字符串值,你必须记住反斜杠逃脱你的双引号在Java程序。请参见下面S的声明。

 一个String =[[\\110917 \\,3.0099999999999998,-0.72999999999999998,2.8500000000000001,2.96,685.0,38603.0],[\\110917 \\,2.71,0.20999999999999999,2.8199999999999998 ,2.8999999999999999,2987.0,33762.0]];

在main()方法你code正常工作。下面是只是在main()方法的code的小的修改。

 的System.out.println(字符串以JSON数组语句);
JsonParser分析器=新JsonParser();
JsonElement tradeElement = parser.parse(S);
JsonArray贸易= tradeElement.getAsJsonArray();
的System.out.println(贸易);

最后,请记住preFIX你的声明com.google.gson。*的关键字进口,如下图所示。

 进口com.google.gson *。

How do you convert this String into gson.JsonArray?

String s= "[["110917    ", 3.0099999999999998, -0.72999999999999998, 2.8500000000000001, 2.96, 685.0, 38603.0], ["110917    ", 2.71, 0.20999999999999999, 2.8199999999999998, 2.8999999999999999, 2987.0, 33762.0]]";

This is my Code:

 com.google.gson.*;
 public static void main(String[] args)
   {
       //Declared S here
       System.out.println("String to Json Array Stmt");
       JsonParser parser = new JsonParser();
       JsonElement tradeElement = parser.parse(s.toString());
       JsonArray trade = tradeElement.getAsJsonArray();
       System.out.println(trade);
    }

Is this the way to convert this Collections string to JSonArray?

解决方案

To have a string value inside your JSON array, you must remember to backslash escape your double-quotes in your Java program. See the declaration of s below.

String s = "[[\"110917       \", 3.0099999999999998, -0.72999999999999998, 2.8500000000000001, 2.96, 685.0, 38603.0], [\"110917    \", 2.71, 0.20999999999999999, 2.8199999999999998, 2.8999999999999999, 2987.0, 33762.0]]";

Your code in the main() method works fine. Below is just a minor modification of your code in the main() method.

System.out.println("String to Json Array Stmt");
JsonParser parser = new JsonParser();
JsonElement tradeElement = parser.parse(s);
JsonArray trade = tradeElement.getAsJsonArray();
System.out.println(trade);

Lastly, remember to prefix your statement "com.google.gson.*" with the keyword "import", as shown below.

import com.google.gson.*;

这篇关于不能将字符串转换为JsonArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆