52loli 发表于 2021-11-10 00:00

Go语言 解析json格式的试题

本帖最后由 52loli 于 2021-11-10 00:04 编辑

```go
package main

import (
        "fmt"
        "net/http"
        "io/ioutil"
        "encoding/json"
        "os"
)

// 选项
type OptionsInfo struct {
        Id string
        OptionContent string
}

type QuestionsInfo struct {
        Id int        // 试题Id
        Options []OptionsInfo
        QuestionStem string        // 题目
}

// 正确答案
type Answers struct {
        Answer []string        // 答案选项
        Id int        //答案Id
}

type Loli struct {
        QuestionAnswers []Answers
        Questions []QuestionsInfo
}

func getData(url string) *[]byte{
        resp, err := http.Get(url)
        if err != nil {
                fmt.Println(err)
                os.Exit(1)
        }
        defer resp.Body.Close()
        body, _ := ioutil.ReadAll(resp.Body)
        return &body
}

func main() {
        var loli Loli
        AnswersInfoMap := make(mapstring)
        loli.QuestionAnswers = make([]Answers, 1)
        loli.QuestionAnswers.Answer = make([]string, 1)
        loli.Questions = make([]QuestionsInfo, 1)
        loli.Questions.Options = make([]OptionsInfo, 1)
        resp := getData("https://obs.cn-north-1.myhuaweicloud.com/ex-adaptor/invigilate/77b78f90a0a243cea90d558254cf8e21.json")
        f, err := os.Create("loli.txt")
        if err != nil {
                fmt.Println(err)
                return
        }
        defer f.Close()
        json.Unmarshal(*resp, &loli)
        // 将正确答案添加进map里
        for _, v := range loli.QuestionAnswers {
                AnswersInfoMap = v.Answer
        }
       
        // 写出试题信息
        for i, v := range loli.Questions {
                f.WriteString(fmt.Sprintf("%s.%s( %s )\n\n", fmt.Sprintf("%d", i + 1), v.QuestionStem, AnswersInfoMap))
                str := ""
                for _, k := range v.Options {
                        str += fmt.Sprintf("%s.%s\n", k.Id, k.OptionContent)
                }
                str += "\n"
                f.WriteString(str)
        }
}
```

https://ae05.alicdn.com/kf/Hf8e1447c75a64aa0a03b8a2957f1d839w.png

最后生成的试题文件 链接: https://pan.baidu.com/s/17o64VqWHWkD_W9AmB2ls4g 提取码: 1bqy

lyhjh 发表于 2021-11-10 08:17

mark学习一下

ynboyinkm 发表于 2021-11-10 08:49

go还没有弄过呢......

cnzefe 发表于 2021-11-10 09:13

谢谢分享,go语言一统天下。

Light紫星 发表于 2021-11-10 13:28

以前用塞班上的mshell语言解析json,没有模块,只好自己写了一个mshel的json模块
//mshell json模块
//by: zixing
//2020-09-22

use array;

class Tokener
jsonstr
_cur_token
i

function init(str)
this.jsonstr = str;
this.i=0;
_cur_token = null;

end;

function cur_char()
if i<len(jsonstr) then
      return substr(jsonstr,i,1);
else
          return "";
end;
end;

function move_i(step=1)
if i<len(jsonstr) then
   i+=step;
end;
end;

function next_string()
outstr = '';
trans_flag = false;
move_i();

while cur_char()#'' do
   ch =cur_char();
   if ch = '\\' then
    trans_flag = true;
   else
        if trans_flag=false then
       if ch = '"' then
          break;
          end;
        else
       trans_flag=false;
        end;
    end;
        outstr = outstr+ch;
        move_i();
end;
return outstr;
end;

function next_number()

expr = '';
ls = ['0','1','2','3','4','5','6','7','8','9','.', '+', '-','e','E'];
while array.index(ls,cur_char())>-1 do
   expr = expr+cur_char();
   move_i();
end;
move_i(-1);
return num(expr);
   
end;

function isalpha(cchar)
   numchar = code(cchar,0);
   if ((numchar>=65) and (numchar<=90)) or ((numchar>=97) and (numchar<=122)) then
    return true;
   else
        return false;
   end;
end;

function next_const()
outstr = '';
while isalpha(cur_char()) do
   outstr = outstr+cur_char();
   move_i();
end;
move_i(-1);

   ls = ['true', 'false', 'null'];
   if array.index(ls,outstr)>-1 then
    t = ['true': true, 'false': false,'null': null];
        return t;
   end;
   
end;

function next()
ls = ['\x20', '\n', '\r', '\t'];
while array.index(ls,cur_char())>-1 do
   move_i();
end;

ch = cur_char();
if ch = '' then
   cur_token = null;
elsif array.index(['{', '}', '[', ']', ',', ':'],ch)>-1 then
   cur_token = ch;
elsif ch = '"' then
   cur_token = next_string();
elsif isalpha(ch) then
   cur_token = next_const();
elsif array.index(['0','1','2','3','4','5','6','7','8','9','.', '+', '-','e','E'],ch)>-1 then
   cur_token = next_number();
else
   print "Invalid symbol "+ch;
end;

move_i();
_cur_token = cur_token;
return cur_token # null;

end;

function cur_token()
//print(_cur_token);
return _cur_token;
end;


end;

class JsonDecoder

function __json_array(tokener:Tokener) forward

function __json_object(tokener:Tokener)
obj = [];
if tokener.cur_token() # '{' then
print('Json must start with "{"');
return;
end;

while true do

tokener.next();
tk_temp = tokener.cur_token();
//print 'tk_temp:'+tk_temp;
if tk_temp = '}' then
   return [];
end;

if not isstr(tk_temp) then
   print 'invalid key ' + tk_temp;
   return;
end;

key = tk_temp;
tokener.next();

if tokener.cur_token()#':' then
   print 'expect ":" after "'+key+'"';
   return;
end;

tokener.next();
val = tokener.cur_token();

   //print 'val:'+val;
   
if val = '[' then
   val = this.__json_array(tokener);
elsif val = '{' then
   val = this.__json_object(tokener);
end;

obj = val;
tokener.next();
tk_split = tokener.cur_token();
if tk_split = ',' then
   a = 0; //continue
else
if tk_split = '}' then
   break;
else
   if tk_split = null then
    print 'missing "}" at at the end of object';
    return;
   end;
   end;
       print 'unexpected token "'+tk_split+'" at key "'+key+'"';
   return;
   
end;
end;

return obj;

end;

function __json_array(tokener:Tokener)

if tokener.cur_token()#'[' then
   print 'Json array must start with "["';
   return;
end;

arr = [];

while true do

   tokener.next();
   tk_temp = tokener.cur_token();
   if tk_temp = ']' then
    return [];
   end;
   
   if tk_temp = '{' then
    val = this.__json_object(tokener);
   elsif tk_temp = '[' then
    val = this.__json_array(tokener);
   elsif array.index([',', ':', '}'],tk_temp)>-1 then
    print 'unexpected token "'+tk_temp+'"' ;
    return;
   else
       val = tk_temp;
   end;
   append(arr,val);
   
   tokener.next();
   tk_end = tokener.cur_token();
   
   if tk_end = ',' then
    a = 1; //continue
   end;
   
   if tk_end = ']' then
    break;
   else
    if tk_end = null then
       print 'missing "]" at the end of array';
       return;
        end;
   end;
end;
return arr;
end;


function decode(json_str)
tokener:Tokener = Tokener(json_str);
if not tokener.next() then
   return null;
end;
first_token = tokener.cur_token();
if first_token = '{' then
   decode_val = this.__json_object(tokener);
elsif first_token = '[' then
   decode_val = this.__json_array(tokener);
else
        print 'Json must start with "{"';
    return;
end;
if tokener.next() then
   print 'unexpected token "'+tokener.cur_token()+'"';
   return;
end;
return decode_val;

end;

function loads(json_str)
return decode(json_str);
end;
end;

ye1209mx 发表于 2021-11-10 15:07

不错,点赞一下

lxwen 发表于 2021-11-10 18:59

感谢你的回答,,我还有同样的json文件9个,是不是把地址替换成上面的,就可以解析出来。。

还有能不能用python实现上面的功能。。@52loli

52loli 发表于 2021-11-10 20:11

lxwen 发表于 2021-11-10 18:59
感谢你的回答,,我还有同样的json文件9个,是不是把地址替换成上面的,就可以解析出来。。

还有能不能 ...

对的,把地址换一下就可以解析了,python肯定也可以的呀
页: [1]
查看完整版本: Go语言 解析json格式的试题