duansaoguan7955
2016-11-11 13:03
浏览 64
已采纳

如何将字符串转换为JSON并将数据保存在数组中?

I'm using this JSON parser to extract data from a JSON response I'm getting from an API. It returns a byte array containing the data and when convert the byte array to a string and print it, I get the following output:

   [{"Name": "Vikings", "Type": "show"}, 
   {"Name": "Spartacus: Gods Of The Arena", "Type": "show"}, 
   {"Name": "True Detective", "Type": "show"}, 
   {"Name": "The Borgias", "Type": "show"}, 
   {"Name": "Se7en", "Type": "movie"}]

Since this is a regular string, I have no way of maniuplating the data to extract whatever I need. Ideally, I'd like to have arrays like these:

   shows := ["Vikings", "Spartacus: Gods Of The Arena"...]
   movies := ["Se7en", "other data", ...]

What I want to do with these arrays is give the user titles based on the type (ie: show, movie, etc) he/she asked for. So essentially what I'm looking for is a way to convert the string in to something that I can easily manipulate (and possibly filter).

I apoligize if this seems like a strange way of doing this, but I can't think of any other way of doing it. I feel like Go's syntax and way of doing things is very unconventional compared to another language like Javascript where I could easily have done this in a line or two.

图片转代码服务由CSDN问答提供 功能建议

我正在使用 JSON解析器,用于从我从API获取的JSON响应中提取数据。 它返回一个包含数据的字节数组,当将字节数组转换为字符串并打印时,我得到以下输出:</ p>

  [{“ Name”:“ Vikings”  ,“类型”:“ show”},
 {“名称”:“斯巴达克斯:竞技场之神”,“类型”:“ show”},
 {“名称”:“真实侦探”,“类型”  :“ show”},
 {“名称”:“ The Borgias”,“类型”:“ show”},
 {“名称”:“ Se7en”,“类型”:“电影”}] 
 <  / code> </ pre> 
 
 

由于这是一个常规字符串,因此我无法处理数据以提取所需的任何内容。 理想情况下,我希望有这样的数组:</ p>

 显示:= [“维京人”,“斯巴达克斯:竞技场之神” ...] 
电影:  = [“ Se7en”,“其他数据”,...] 
 </ code> </ pre> 
 
 

我要对这些数组进行的操作是根据类型为用户提供标题( 例如:表演,电影等)。 因此,本质上,我正在寻找的是一种将字符串转换为可以轻松操作(并可能进行过滤)的方法。 </ p>

如果这看起来像是一种奇怪的方式,我会公开声明,但是我想不出其他任何方式。 与其他语言(例如Javascript)相比,我可以很容易地在一两行中完成Go的语法和处理方式,这与常规语言非常不同。 </ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题